How to reset recyclerView position item views to o

2020-06-28 00:50发布

I have a RecyclerView with rows that have views that when clicked will be disabled for that row position.

The problem is after I update the adapter like this:

    adapterData.clear();
    adapterData.addAll(refreshedAdapterData);
    notifyDataSetChanged();

After refreshing the data, the disabled views at the previous recycler position still remain disabled even though the data is refreshed. How can I reset the views to the original state after refreshing adapter data.

3条回答
别忘想泡老子
2楼-- · 2020-06-28 01:17

When you call notifyDataSetChanged(), the onBindViewHolder() method of every view is called. So you could add something like this in the onBindViewHolder() of your Adapter method:

@Override   
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
            if (refreshedAdapterData.get(position).isInDefaultState()) {
                //set Default View Values
            } else {
                //the code you already have
            }

        }
查看更多
劫难
3楼-- · 2020-06-28 01:26

I have resolved this by putting a conditional statement inside onBindViewHolder method instructing all positions to reset the disabled views if data meets the required conditions for a refreshed data.

@Christoph Mayr, thanks for your comments. It helped point me in the right direction.

查看更多
一夜七次
4楼-- · 2020-06-28 01:39

Use below code.

  adapterData.clear();
adapterData.addAll(refreshedAdapterData);

adapter.notifyDataSetChanged();

OR

recyclerView.invalidate();
查看更多
登录 后发表回答