Ask a question about the confusion of RecyclerView data.

when I click like, the like icon turns blue, and then the number of likes + 1. But loading more will lead to data confusion. For example, what I like now is the first one, but after loading more, it will change to the third.
here is the effect picture and code:

CommentsController class:

public class CommentsController extends TabController implements BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.rv_comments)
    RecyclerView mRecyclerView;
    @BindView(R.id.rl_comments_empty_view)
    RelativeLayout mEmptyView;
    @BindView(R.id.rl_comments_loading)
    RelativeLayout mLoading;

    private static final String TAG = "CommentsController";
    private Context mContext;
    private List<CommentsBean> mDatas;
    private CommentsAdapter mAdapter;
    private String mVid;
    private int mListSize;
    private int mSize = 1;

    public CommentsController(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected View initContentView(Context context) {
        View view = View.inflate(context, R.layout.controller_comments, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData() {
        getInitData();
    }

    public void getInitData() {
        mSize = 1;//
        mListSize = 0;
        mLoading.setVisibility(View.VISIBLE);
        mVid = (String) BaseApplication.getApplication().getMap().get("vid");
        mDatas = new ArrayList<>();
        mAdapter = new CommentsAdapter(R.layout.item_comments, mDatas, mContext);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        getCommentsData();
    }

    /**
     * 
     */
    private void getCommentsData() {
        ApiManager.getService()
                .commentsList(NetWorkRequestUtils.createRequestBody(new CommentsObject()))
                .compose(RxUtils.<BaseResponse<List<CommentsBean>>>schedulers((Activity) mContext))
                .subscribe(new HttpCallback<List<CommentsBean>>((Activity) mContext) {
                    @Override
                    public void onSuccess(List<CommentsBean> commentsBeans, String msg) {
                        mListSize += commentsBeans.size();
                        Log.d(TAG, "onSuccess: 1506= mListSize " + mListSize + ", commentsBeans=" + commentsBeans);
                        if (commentsBeans.size() >= 10)
                            mAdapter.setOnLoadMoreListener(CommentsController.this, mRecyclerView);//10
                        if (mListSize == 0 && commentsBeans.isEmpty()) {
                            mEmptyView.setVisibility(View.VISIBLE);
                        } else if (commentsBeans != null && !ResponseUtils.isDataEnd(commentsBeans)) {
                            mSizePP;
                            mDatas.addAll(commentsBeans);
                            mAdapter.notifyDataSetChanged();
                            mAdapter.loadMoreComplete();
                        } else {
                            mDatas.addAll(commentsBeans);
                            mAdapter.notifyDataSetChanged();
                            mAdapter.loadMoreEnd();
                        }
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        mLoading.setVisibility(View.GONE);
                        if (mListSize > 0)
                            mEmptyView.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    public void onLoadMoreRequested() {
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                getCommentsData();
            }
        }, 500);
    }

    private class CommentsObject {
        String vid = mVid;
        int p = mSize;
        int type = 1;
    }
}

CommentsAdapter class:

public class CommentsAdapter extends BaseQuickAdapter<CommentsBean, BaseViewHolder> {

    private Context mContext;
    private String mVid;
    private final String mToken;
    private boolean isLike;

    public CommentsAdapter(int layoutResId, @Nullable List<CommentsBean> data, Context context) {
        super(layoutResId, data);
        this.mContext = context;
        mVid = (String) BaseApplication.getApplication().getMap().get("vid");
        mToken = PreferenceUtils.getString(mContext, "token");
        Log.d(TAG, "CommentsAdapter: 1440= vid=" + mVid + ",    token=" + mToken);
    }

    @Override
    protected void convert(BaseViewHolder helper, CommentsBean item) {
        helper.setText(R.id.tv_comments_username, item.getNickname())
                .setText(R.id.tv_comments_date, item.getCtime())
                .setText(R.id.tv_comments_content, item.getContent());
        Glide.with(mContext).load(item.getHeadimg()).crossFade()
                .transform(new GlideRoundTransformUtils(mContext))//
                .into((ImageView) helper.getView(R.id.iv_comments_avatar));
        int position = helper.getLayoutPosition();
        LikeButton likeButton = helper.getView(R.id.comments_like_button);//
        TextView tvLikeCount = helper.getView(R.id.tv_video_comments_like_count);//
        likeComments(position, likeButton, tvLikeCount);//

//        boolean like = item.isLike();
//        if(like){
//            likeButton.setLiked(true);
//        }else{
//            likeButton.setLiked(false);
//        }
    }

    private void likeComments(final int position, LikeButton likeButton, final TextView tvLikeCount) {
//        likeButton.setEnabled(true);//,false,true,
        boolean fastDoubleClick = FastClickUtils.isFastDoubleClick();//TODO:
//            likeButton.setLiked(true);//TODO:

        likeButton.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                //TODO:
                Toast.makeText(mContext, "" + position, Toast.LENGTH_SHORT).show();
                int likeCount = Integer.parseInt(tvLikeCount.getText().toString().trim());
                int liked = likeCount + 1;
                tvLikeCount.setText(liked + "");
            }

            @Override
            public void unLiked(LikeButton likeButton) {
                //TODO:
                int likeCount = Integer.parseInt(tvLikeCount.getText().toString().trim());
                int liked = likeCount - 1;
                tvLikeCount.setText(liked + "");
            }
        });
    }
}
Mar.09,2021

this is the same as the loading mode of listview. If you set a change, be sure to set the data for those that are not set. For example, if you set the first item that is not blue, then the other should be set to black. Don't set it because it is black by default.

listview and RecyclerView are both visible refresh modes, for example, in this article:
https://www.2cto.com/kf/20160.

officials have done relevant sharing in the early listview era, probably in 2014, it is not possible to test, roughly understand why, and then the solution is relatively simple, in general, it is necessary to record data for all projects to judge the presentation. Confusion will occur if it is not recorded, because it is a way of layout reuse.

Menu