鍍金池/ 問(wèn)答/Android/ 請(qǐng)教一個(gè)RecyclerView數(shù)據(jù)錯(cuò)亂的問(wèn)題

請(qǐng)教一個(gè)RecyclerView數(shù)據(jù)錯(cuò)亂的問(wèn)題

當(dāng)我點(diǎn)擊點(diǎn)贊的時(shí)候,贊的圖標(biāo)變成藍(lán)色,然后該點(diǎn)贊數(shù)+1.但是加載更多會(huì)出現(xiàn)數(shù)據(jù)錯(cuò)亂.比如我現(xiàn)在點(diǎn)贊的是第一條,但是加載更多后會(huì)變到第三條去了.
下面是效果圖和代碼:
圖片描述

CommentsController類:

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;//切換視頻列表再切換評(píng)論需重置
        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();
    }

    /**
     * 獲取初始化評(píng)論列表數(shù)據(jù)
     */
    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條數(shù)據(jù)才去加載更多
                        if (mListSize == 0 && commentsBeans.isEmpty()) {
                            mEmptyView.setVisibility(View.VISIBLE);
                        } else if (commentsBeans != null && !ResponseUtils.isDataEnd(commentsBeans)) {
                            mSize++;
                            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類:

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))//將圖片轉(zhuǎn)為圓形
                .into((ImageView) helper.getView(R.id.iv_comments_avatar));
        int position = helper.getLayoutPosition();
        LikeButton likeButton = helper.getView(R.id.comments_like_button);//點(diǎn)贊按鈕
        TextView tvLikeCount = helper.getView(R.id.tv_video_comments_like_count);//點(diǎn)贊數(shù)
        likeComments(position, likeButton, tvLikeCount);//點(diǎn)贊評(píng)論

//        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);//點(diǎn)贊開(kāi)關(guān),false為禁止點(diǎn)贊,默認(rèn)true,用于無(wú)網(wǎng)絡(luò)時(shí)禁止點(diǎn)贊
        boolean fastDoubleClick = FastClickUtils.isFastDoubleClick();//TODO:避免快速點(diǎn)擊發(fā)起多次請(qǐng)求
//            likeButton.setLiked(true);//TODO:加載更多后數(shù)據(jù)錯(cuò)亂

        likeButton.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                //TODO:提交點(diǎn)贊結(jié)果到后臺(tái)
                Toast.makeText(mContext, "點(diǎn)擊了" + 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:提交取消點(diǎn)贊結(jié)果到后臺(tái)
                int likeCount = Integer.parseInt(tvLikeCount.getText().toString().trim());
                int liked = likeCount - 1;
                tvLikeCount.setText(liked + "");
            }
        });
    }
}
回答
編輯回答
過(guò)客

這和listview的加載模式是一樣的,設(shè)置了變動(dòng)的一定記得給未設(shè)置的也設(shè)置數(shù)據(jù),比如:你設(shè)置第一條未藍(lán)色,那么其他要設(shè)置黑色,不要因?yàn)槟J(rèn)是黑色就不設(shè)置了。

listview和RecyclerView都是可見(jiàn)刷新的模式,比如這個(gè)文章的說(shuō)法:
https://www.2cto.com/kf/20160...

官方在早期listview的時(shí)代有做過(guò)相關(guān)分享,可能是2014年的時(shí)候,不可考,大致清楚為什么,然后解決的方式就比較簡(jiǎn)單了,總得來(lái)說(shuō)就是對(duì)所有的項(xiàng)目都要記錄數(shù)據(jù)用以對(duì)展示進(jìn)行判斷。如果不記錄就會(huì)出現(xiàn)錯(cuò)亂,因?yàn)槭遣季謴?fù)用的方式。

2017年4月16日 01:00