简体   繁体   English

onBindViewHolder 未触发回收站视图中的项目

[英]onBindViewHolder not triggering for items in recycler view

I have a recycler view with 10 items.我有一个包含 10 个项目的回收站视图。 I scroll to the bottom of the page, and now I'm trying to call notifyItemChanged(pos) for the first item which is not visible since I scrolled down.我滚动到页面底部,现在我正在尝试为第一个项目调用notifyItemChanged(pos) ,因为我向下滚动后该项目不可见。 When I call this, it is not triggering onBindViewholder() for the first item.当我调用它时,它不会触发第一项的onBindViewholder() So in short my question is when I call notifyItemChanged(pos) with a position that is not displaying on the screen, is it not triggering the onBindViewHolder() ?.所以简而言之,我的问题是当我使用未显示在屏幕上的 position 调用notifyItemChanged(pos)时,它不会触发onBindViewHolder()吗? This is my adapter code这是我的适配器代码

@SuppressLint("NewApi")
    @Override
    public void onBindViewHolder(@NonNull final UserTasksViewHolder holder, final int position) {
        if (usertasks != null) {
            final UserSessionTasksEntity current = usertasks.get(position);
            viewPos = prefs.getString(view_position, "");
            holder.taskCode.setText(current.getTaskCode());
            holder.taskName.setText(current.getTaskName());
    }

    @Override
    public int getItemCount() {
        if (usertasks != null)
            return usertasks.size();
        return 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

Your error is on your onItemCount method.您的错误在于您的 onItemCount 方法。 NotifydatasetChanged calls onBindViewHolder for each of the items in your usertasks and goes as far as your usertasks.size. NotifydatasetChanged 为您的 usertasks 中的每个项目调用 onBindViewHolder,并一直到您的 usertasks.size。 Change your onGetItemCount to this:将您的 onGetItemCount 更改为:

@Override
public int getItemCount() {
    if (usertasks != null)
        return usertasks.size();
    else{
       return 0;
    }
     
}

Now your notifyDatasetChanged will call onBindViewHolder as long as your usertasks is not null.现在,只要您的 usertasks 不是 null,您的 notifyDatasetChanged 就会调用 onBindViewHolder。 If it is still not calling, then look for your usertasks as it is probably null如果仍然没有调用,请查找您的用户任务,因为它可能是 null

First create a method in recycler view adapter class, like to update:首先在recycler视图适配器class中创建一个方法,喜欢更新:

public void setData(ArrayList<GridItem> newitems,int pos){ // this is new array
        this.mydata=newitems;//here you update your orignal data
        this.notifyItemChanged(pos);//here notify the change at particular position

    }

In your fragment class:在您的片段 class 中:

//get your recyclerview  adapter and call the above method
adapter.setData(newdata,position);

I was able to fix that.我能够解决这个问题。 The issue is bind view holder is never triggered for the items that were already recycled.问题是永远不会为已经回收的项目触发绑定视图持有者。 Since my logic was written in bind view holder it didn't work, instead I updated my list in the fragment and called notifyDataSetChanged.由于我的逻辑是在绑定视图持有者中编写的,因此它不起作用,而是我更新了片段中的列表并调用了 notifyDataSetChanged。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM