我正在製作奧拉喜歡汽車選擇功能。我有一個API,我從中獲取車型,選定的圖像鏈接和未選中的圖像鏈接。我正在使用帶有文本和圖像視圖的recyclerview。在創建回收站視圖時,我選擇第一個位置並顯示hover-image
以顯示選定的位置。當我選擇另一張圖像時,它會變爲選定的圖像,但之前選擇的圖像不會更改爲未選擇的圖像。這意味着for循環不起作用。但是當我打印未選定圖像的url鏈接時,url鏈接顯示正確。那爲什麼它不在畢加索展出。RecyclerView點擊更改圖片
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Model> arrayList;
private Context context;
public MyAdapter(Context context, List<Model> arrayList) {
this.arrayList = arrayList;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
return new MyViewHolder(itemView);
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView txt;
ImageView image;
LinearLayout LL_root;
MyViewHolder(View view) {
super(view);
txt = (TextView) view.findViewById(R.id.txt);
image = (ImageView) view.findViewById(R.id.image);
LL_root = (LinearLayout) view.findViewById(R.id.LL_root);
LL_root.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.LL_root:
int pos = getAdapterPosition();
System.out.println("sammy_onclick_position "+pos);
for(int i=0; i<arrayList.size(); i++){
if(pos!=i){
Picasso.with(context).load(arrayList.get(i).getImage()).into(image);
System.out.println("sammy_unselected_image "+arrayList.get(i).getImage());
}
}
Picasso.with(context).load(arrayList.get(pos).getSelectedImage()).into(image);
System.out.println("sammy_selected_image "+arrayList.get(pos).getSelectedImage());
break;
}
}
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Model model = arrayList.get(position);
holder.txt.setText(model.getTitle());
if (!TextUtils.isEmpty(model.getImage())){
if(position==0)
Picasso.with(context).load(model.getSelectedImage()).into(holder.image);
else
Picasso.with(context).load(model.getImage()).into(holder.image);
}
}
@Override
public int getItemCount() {
return arrayList.size();
}
}
你試過 - 'notifyDataSetChanged()'? –
我在上次點擊時嘗試notifyDataSetChanged()。但它只調用'onBindViewHolder',這又使得在onBindViewHolder中選擇第一個位置。我只在第一個位置顯示所選圖像 –