问题
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