这是网上贴上的一张图片
1.第一种简单粗暴,直接在viewholder设置如下代码即可,但性能可能大打折扣
innerFragmentMyOderByViewHolder.setIsRecyclable(false);
2.第二种,推荐使用,在需要使用的viewholder 中对使用到的实体bean增加需要设置的状态字段,如user 中booeal ischeck=false,当点击该item中改变ischeck的值为true,在数据赋值的时候先判断是否选中,然后在设置不同状态。
实例: //点击的时候改变字段的值 public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) { //复用后状态错乱.需要点击对应item的关注改变状态 TextView tvNotFocus = view.findViewById(R.id.tv_related_theme_recommended_focus);//默认未关注 String focus = tvNotFocus.getText().toString(); List<TopicRelatedRecommendedBean> data = getData(); TopicRelatedRecommendedBean topicRelatedRecommendedBean = data.get(position); boolean select = topicRelatedRecommendedBean.isSelect(); Log.d(TAG, "1145= "+select); if (view.getId() == R.id.tv_related_theme_recommended_focus) { topicRelatedRecommendedBean.isAttention = !topicRelatedRecommendedBean.isAttention; notifyDataSetChanged(); } } //填充数据的时候判断 @Override public void onBindViewHolder(RecyclerHolder holder, int position) { List<TopicRelatedRecommendedBean> data = getData(); TopicRelatedRecommendedBean topicRelatedRecommendedBean = data.get(position); holder.tvNotFocus.setText(topicRelatedRecommendedBean.isAttention ? "关注" : "已关注"); holder.tvNotFocus.setBackgroundResource(topicRelatedRecommendedBean.isAttention ? R.drawable.topic_already_focus_shape : R.drawable.release_button_shape); }
3.这三种是自己的做法,暂时还没发现bug,也合适那些不想写那么复杂,性能还可以,可以试试,我这种直接在复用时对item中需要改变的状态进行处理,不管什么条件下,只要不同满足条件就对控件进行不同的对应状态即可。如已选中状态设置后,在对应的条件下也设置未选中即可。,严格来说就是if else一定要准确,否则造成数据错乱很容易,不要只写if而不写else