Android development, how to solve the data confusion after RecyclerView reuse?


this is a RecyclerView, that slides from left to right. When I click on the first item follower, it becomes "followed", but when I slide to the right, some of the item data also change to "followed", and the data is still messed up when I slide back.

this is how I handle it, but I can"t:

 public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        //TODO:bug,.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) {
            if (focus.equals("")) {
                tvNotFocus.setText("");
                tvNotFocus.setBackgroundResource(R.drawable.topic_already_focus_shape);
            } else if (focus.equals("")) {
                tvNotFocus.setText("");
                tvNotFocus.setBackgroundResource(R.drawable.release_button_shape);
            }
        }
//        if (select) {
//            Toast.makeText(mContext, "", Toast.LENGTH_SHORT).show();
//            topicRelatedRecommendedBean.setSelect(false);
//            tvNotFocus.setText("");
//            tvNotFocus.setBackgroundResource(R.drawable.release_button_shape);
//        } else {
//            Toast.makeText(mContext, "", Toast.LENGTH_SHORT).show();
//            topicRelatedRecommendedBean.setSelect(true);
//            tvNotFocus.setText("");
//            tvNotFocus.setBackgroundResource(R.drawable.topic_already_focus_shape);
//        }
    }
Mar.02,2021

add a field to your bean that you want to follow or not, change the value of the field when you click, determine whether you care or not when the data is populated, and then set a different state. Here is the virtual code

    //
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        //TODO:bug,.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);
    }
Menu