Disable RecyclerView Recycling in Android
To prevent RecyclerView from being reused, this can be achieved by using the following method:
- Override the getItemViewType method in the RecyclerView Adapter to ensure that each item returns a different ViewType, so that the RecyclerView does not reuse the same type of item.
@Override
public int getItemViewType(int position) {
return position;
}
- Disallow the recycling of child views by setting setRecycleChildrenOnDetach(false) in the RecyclerView’s LayoutManager.
recyclerView.setLayoutManager(new LinearLayoutManager(context) {
@Override
public boolean supportsPredictiveItemAnimations() {
return false;
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
//禁止回收子View
setRecycleChildrenOnDetach(false);
super.onLayoutChildren(recycler, state);
}
});
By following the above method, the reusing effect of the RecyclerView can be disabled.