如何在 recyclerview viewHolder 中使用 picasso?
how use picasso in a recyclerview viewHolder?
我正在尝试在我的项目中使用 Picasso 而不是 R.drawable,我正在尝试更改代码,但我发现很难将 Picasso 与我的 Viewholder 一起使用,也许你们可以帮助我并告诉我如何更改我的代码以使用 Picasso。
java.class
public class RibeiraDePena_PT extends AppCompatActivity
{
DrawerLayout drawerLayout;
Toolbar toolbar;
NavigationView navigationView;
RecyclerView recyclerView;
RecyclerView.Adapter programAdapter;
RecyclerView.LayoutManager layoutmanager;
//Indicar Titulo, descricao e imagem(mesmo tamanho)
String[] programTituloList =
{
"Ponte Romana / do Trajano","Praça de Camões"
};
String[] programDescList =
{
"Descricao-Ponte",
"Descricao-praça camoes hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
};
int [] programImages =
{
R.drawable.chaves_estelas,R.drawable.chaves_estelas
};
String[] Url =
{
"https://www.google.com","https://www.google.com"
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ribeira_tr);
recyclerView = findViewById(R.id.recyclerview);
layoutmanager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutmanager);
programAdapter = new ProgramAdapter(this, programTituloList, programDescList, programImages, Url);
recyclerView.setAdapter(programAdapter);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationview);
navigationView.setItemIconTintList(null);
drawerLayout = findViewById(R.id.drawerlayout);
setSupportActionBar(toolbar);
navigationView = findViewById(R.id.navigationview);
toolbar = findViewById(R.id.toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.navigration_open, R.string.navigration_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
adpater.class
public class ProgramAdapter extends RecyclerView.Adapter<ProgramAdapter.ViewHolder> {
Context context;
String[] programTituloList;
String[] programDescList;
String[] Url;
int[] programImages;
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView rowTitulo;
TextView rowDesc;
ImageView rowImage;
public ViewHolder(@NonNull View itemView) {
super(itemView);
rowTitulo = itemView.findViewById(R.id.txt_titulo);
rowDesc = itemView.findViewById(R.id.txt_desc);
rowImage = itemView.findViewById(R.id.imagem_box);
}
}
public ProgramAdapter(Context context, String[] programTituloList,
String[] programDescList, int[] images, String[] Url) {
this.context = context;
this.programTituloList = programTituloList;
this.programDescList = programDescList;
this.programImages = images;
this.Url = Url;
}
@NonNull
@Override
public ProgramAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.box, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public int getItemCount() {
return programTituloList.length;
}
@Override
public void onBindViewHolder(@NonNull ProgramAdapter.ViewHolder holder, int position) {
holder.rowTitulo.setText(programTituloList[position]);
holder.rowDesc.setText(programDescList[position]);
holder.rowImage.setImageResource(programImages[position]);
holder.rowImage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(Url[holder.getBindingAdapterPosition()]));
context.startActivity(intent);
}
});
}
我成功地在 Viewholder 之外使用了 Picasso。
将 onBindViewHolder 中的 holder.rowImage.setImageResource(programImages[position]);
替换为
Picasso.get().load(programImages[position]).into(holder.rowImage);
我正在尝试在我的项目中使用 Picasso 而不是 R.drawable,我正在尝试更改代码,但我发现很难将 Picasso 与我的 Viewholder 一起使用,也许你们可以帮助我并告诉我如何更改我的代码以使用 Picasso。
java.class
public class RibeiraDePena_PT extends AppCompatActivity
{
DrawerLayout drawerLayout;
Toolbar toolbar;
NavigationView navigationView;
RecyclerView recyclerView;
RecyclerView.Adapter programAdapter;
RecyclerView.LayoutManager layoutmanager;
//Indicar Titulo, descricao e imagem(mesmo tamanho)
String[] programTituloList =
{
"Ponte Romana / do Trajano","Praça de Camões"
};
String[] programDescList =
{
"Descricao-Ponte",
"Descricao-praça camoes hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
};
int [] programImages =
{
R.drawable.chaves_estelas,R.drawable.chaves_estelas
};
String[] Url =
{
"https://www.google.com","https://www.google.com"
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ribeira_tr);
recyclerView = findViewById(R.id.recyclerview);
layoutmanager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutmanager);
programAdapter = new ProgramAdapter(this, programTituloList, programDescList, programImages, Url);
recyclerView.setAdapter(programAdapter);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationview);
navigationView.setItemIconTintList(null);
drawerLayout = findViewById(R.id.drawerlayout);
setSupportActionBar(toolbar);
navigationView = findViewById(R.id.navigationview);
toolbar = findViewById(R.id.toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.navigration_open, R.string.navigration_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
adpater.class
public class ProgramAdapter extends RecyclerView.Adapter<ProgramAdapter.ViewHolder> {
Context context;
String[] programTituloList;
String[] programDescList;
String[] Url;
int[] programImages;
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView rowTitulo;
TextView rowDesc;
ImageView rowImage;
public ViewHolder(@NonNull View itemView) {
super(itemView);
rowTitulo = itemView.findViewById(R.id.txt_titulo);
rowDesc = itemView.findViewById(R.id.txt_desc);
rowImage = itemView.findViewById(R.id.imagem_box);
}
}
public ProgramAdapter(Context context, String[] programTituloList,
String[] programDescList, int[] images, String[] Url) {
this.context = context;
this.programTituloList = programTituloList;
this.programDescList = programDescList;
this.programImages = images;
this.Url = Url;
}
@NonNull
@Override
public ProgramAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.box, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public int getItemCount() {
return programTituloList.length;
}
@Override
public void onBindViewHolder(@NonNull ProgramAdapter.ViewHolder holder, int position) {
holder.rowTitulo.setText(programTituloList[position]);
holder.rowDesc.setText(programDescList[position]);
holder.rowImage.setImageResource(programImages[position]);
holder.rowImage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(Url[holder.getBindingAdapterPosition()]));
context.startActivity(intent);
}
});
}
我成功地在 Viewholder 之外使用了 Picasso。
将 onBindViewHolder 中的 holder.rowImage.setImageResource(programImages[position]);
替换为
Picasso.get().load(programImages[position]).into(holder.rowImage);