Mike U
Mike U

Reputation: 236

Recycler View to change View Holders

I use a RecyclerView to display a list of items. The RecyclerView shows a single item at the time. By clicking a button I would like to change ViewHolders for all the items, including the one displayed. The data stays the same, only the list item layout changes.

I supposed I need to clear the cache pool, but it did not help. There are still views in the recycler pool.

recyclerView.recycledViewPool.clear()

RecyclerView keeps using the cached views. Moreover, how to re-create the view with a new ViewHolder of the item displayed?

Upvotes: 0

Views: 924

Answers (1)

Muhammad Ahmed
Muhammad Ahmed

Reputation: 1048

add type in your model class

var viewType : ViewType

make an enum of viewType

ViewType { VIEW_ONE, VIEW_TWO }

override ItemViewType function in your RecyclerViewAdapter. Make separate Layout files for each view type and create/inflate in onCreateViewHolder of RecyclerViewAdapter.

When button is being pressed. Change the ViewType in your model class and call notifyDatasetChanged()

Upvotes: 1

Related Questions