在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Android/ 請教一個RecyclerView數(shù)據(jù)錯亂的問題

請教一個RecyclerView數(shù)據(jù)錯亂的問題

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

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

    /**
     * 獲取初始化評論列表數(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);//點贊按鈕
        TextView tvLikeCount = helper.getView(R.id.tv_video_comments_like_count);//點贊數(shù)
        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);//點贊開關(guān),false為禁止點贊,默認true,用于無網(wǎng)絡(luò)時禁止點贊
        boolean fastDoubleClick = FastClickUtils.isFastDoubleClick();//TODO:避免快速點擊發(fā)起多次請求
//            likeButton.setLiked(true);//TODO:加載更多后數(shù)據(jù)錯亂

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

這和listview的加載模式是一樣的,設(shè)置了變動的一定記得給未設(shè)置的也設(shè)置數(shù)據(jù),比如:你設(shè)置第一條未藍色,那么其他要設(shè)置黑色,不要因為默認是黑色就不設(shè)置了。

listview和RecyclerView都是可見刷新的模式,比如這個文章的說法:
https://www.2cto.com/kf/20160...

官方在早期listview的時代有做過相關(guān)分享,可能是2014年的時候,不可考,大致清楚為什么,然后解決的方式就比較簡單了,總得來說就是對所有的項目都要記錄數(shù)據(jù)用以對展示進行判斷。如果不記錄就會出現(xiàn)錯亂,因為是布局復(fù)用的方式。

2017年4月16日 01:00