请教一个RecyclerView数据错乱的问题

202005021116 应用层 2018-05-05 11:10:22
当我点击点赞的时候,赞的图标变成蓝色,然后该点赞数+1.但是加载更多会出现数据错乱.比如我现在点赞的是第一条,但是加载更多后会变到第三条去了.
下面是效果图和代码:


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();
}

/**
* 获取初始化评论列表数据
*/
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)) {
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))//将图片转为圆形
.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 + "");
}
});
}
}
...全文
1631 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿飞__ 2018-05-07
  • 打赏
  • 举报
回复
我看了一下,你要在 likeButton 的点击事件里面,设置 item 的 isLike 属性呀
202005021116 应用层 2018-05-07
  • 打赏
  • 举报
回复
引用 1 楼 afei__ 的回复:
数据错乱的原因是 RecyclerView 的子 View 重复利用导致的。 你在 convert 方法中把注释掉的代码打开后不能解决吗
试过不行
bt侠 2018-05-07
  • 打赏
  • 举报
回复
2种方式,1种傻瓜式,在dapter内绑定viewholder方法内直接设置mHolder.setIsRecyclable(false); ,为禁止复用,缺点是条目过多会导致消耗资源过多. 第2种就是在额外添加字段去保存当前item的一些内容,然后在viewholder去展示页面的时候获取之前保存的内容,再进行展示,好处是不会混乱,缺点就是需要每次显示都要获取数据去显示,并且代码量多了点.
阿飞__ 2018-05-07
  • 打赏
  • 举报
回复
item 添加一个字段记录点赞数,或者把 item 中的 isLike 从布尔值改为 int 值,int 值的大小表示点赞数,等于0的时候就表示没有点赞
202005021116 应用层 2018-05-07
  • 打赏
  • 举报
回复
引用 3 楼 afei__ 的回复:
我看了一下,你要在 likeButton 的点击事件里面,设置 item 的 isLike 属性呀
现在我改成这样写,点赞图标是正常了,但是加载更多后点赞数还是会错乱.
    @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_video_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, item);//点赞评论

        likeButton.setLiked(item.isLike() ? true : false);//防止加载更多后点赞图标错乱

        //TODO:点赞数加载更多后会错乱
//        boolean add = item.isAdd();//点赞数
//        item.setAdd(add ? true : false);
    }

    private void likeComments(final int position, LikeButton likeButton, final TextView tvLikeCount, final CommentsBean item) {
//        likeButton.setEnabled(true);//点赞开关,false为禁止点赞,默认true,用于无网络时禁止点赞
        boolean fastDoubleClick = FastClickUtils.isFastDoubleClick();//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 + "");
                item.setLike(true);//防止加载更多后点赞图标错乱

//                item.setAdd(true);
            }

            @Override
            public void unLiked(LikeButton likeButton) {
                //TODO:提交取消点赞结果到后台
                int likeCount = Integer.parseInt(tvLikeCount.getText().toString().trim());
                int liked = likeCount - 1;
                tvLikeCount.setText(liked + "");
                item.setLike(false);//防止加载更多后点赞图标错乱
//                item.setAdd(false);
            }
        });
    }
阿飞__ 2018-05-06
  • 打赏
  • 举报
回复
数据错乱的原因是 RecyclerView 的子 View 重复利用导致的。 你在 convert 方法中把注释掉的代码打开后不能解决吗

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧