How to reset recyclerView position item views to original state after refreshing adapter

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-22 16:43:06

问题


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.


回答1:


Use below code.

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

adapterData.notifyDataSetChanged();

OR

recyclerView.invalidate();



回答2:


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:


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.



来源:https://stackoverflow.com/questions/43245115/how-to-reset-recyclerview-position-item-views-to-original-state-after-refreshing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!