我使用RecyclerView显示包含Image和TextView作为rawitem的数据。
我的适配器类如下:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.RecyclerViewHolders> {
private List<MyModel> itemList;
private Context context;
private DisplayImageOptions displayImageOptions;
public MyAdapter(Activity context, List<MyModel> itemList) {
this.itemList = itemList;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.singleview_grid_location, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(final RecyclerViewHolders holder, final int position) {
holder.rl_main.setTag(position);
final MyModel mainModel = itemList.get((int) holder.rl_main.getTag());
if (mainModel.is_selected()) {
holder.rl_main.setBackgroundResource(R.drawable.border_theme);
holder.iv_selection.setVisibility(View.VISIBLE);
} else {
holder.rl_main.setBackgroundColor(Color.parseColor("#FFFFFF"));
holder.iv_selection.setVisibility(View.GONE);
}
holder.txt_location_name.setText(mainModel.getLocationName());
if (mainModel.getImagePath() != null && !mainModel.getImagePath().equals("")) {
Constant.getImageLoader().displayImage(mainModel.getImagePath(),
holder.iv_location_photo, Constant.getInstanceImageOptions(R.mipmap.popup_placeholder), new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view2) {
}
@Override
public void onLoadingFailed(String imageUri, View view2, FailReason failReason) {
holder.iv_location_photo.setImageResource(R.mipmap.popup_placeholder);
}
@Override
public void onLoadingComplete(String imageUri, View view2, Bitmap loadedImage) {
}
});
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mainModel.is_selected()) {
mainModel.setIs_selected(false);
} else {
mainModel.setIs_selected(true);
}
itemList.set(position, mainModel);
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return this.itemList.size();
}
public class RecyclerViewHolders extends RecyclerView.ViewHolder {
public TextView txt_location_name;
public ImageView iv_location_photo;
public CardView card_view;
public ImageView iv_selection;
public RelativeLayout rl_main;
public RecyclerViewHolders(View itemView) {
super(itemView);
txt_location_name = (TextView) itemView.findViewById(R.id.txt_location_name);
iv_location_photo = (ImageView) itemView.findViewById(R.id.iv_location_photo);
card_view = (CardView) itemView.findViewById(R.id.card_view);
iv_selection = (ImageView) itemView.findViewById(R.id.iv_selection);
rl_main = (RelativeLayout) itemView.findViewById(R.id.rl_main);
}
}
}
问题在于,当我点击Recyclerview的视图时,图像发生了变化。 Ya,textview和我的选择保持不变并且工作准确。问题是图像被更改为视图。
可能是什么问题?请结账。感谢。
答案 0 :(得分:0)
这样做有什么意义?
holder.rl_main.setTag(position);
final MyModel mainModel = itemList.get((int) holder.rl_main.getTag());
您需要从onBindView为您提供的位置参数本身获取List位置。 接下来你应该在Class上编写onClickListener而不是在onBindView.Bad实践中实现,并且可能会产生一些问题。 覆盖方法。 “getItemViewType(int position)”。 修改模型后。 您应该根据需要和getAdapterPosition提供的提供位置来执行添加/删除/更改/插入的通知项目。