Maintain scroll position in nested RecyclerViews - android

I have a ViewHolder that contains another RecyclerView. This type of the ViewHolder gets reused multiple times.
Since the ViewHolder is shared between different items, the scroll position of the RecyclerView is also shared between items. Eg, You scroll to the end of the first item, the fifth item's RecyclerView also gets scrolled to the very end, because the view is reused.
I'm trying to solve this by mapping 1 ViewHolder to 1 item.
I've tried to set an itemId for the item, call RecyclerView.ViewHolder#setIsRecyclable(false), and extend/set RecyclerView.getRecycledViewPool().setMaxRecycledViews() as mentioned in this answer
After I scroll down and scroll back up (The ViewHolder gets recycled), the scroll position of the inner RecyclerView is reset to the first item.

Related

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?
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()

RecyclerView items reset when scrolling down

I'm using RecyclerView with expandable list items. The problem is that when I expand an item and scroll the items off-screen gets reset like this:
Is there any way to fix this and preserve the item's state?
Thanks in advance.
You need the object in your ListAdapter to store the expansion state. It may be that you need to create a new class that wraps the existing data and adds a boolean indicating if the row is expanded. Then, when you re-initialize the view as the list is scrolled, you can set the view to be expanded or collapsed according to the data in the adapter. The view itself can't hold this state for you because the view object is "recycled" to display a different record as the user scrolls.

Nested RecyclerView scrolls to the first item on NotifyItemChanged

I have vertical RecyclerView for scrolling group of items and horizontal RecyclerView in each ViewHolder in order to scrol items inside of these groups. They are populated from database. Whenever item content is changed (user tap something or new data come from network) it is written to database and then notifyDataSetChanged() is called for the group cursor. I check if it is the same group in onBingViewHolder() and update items only if it is. But horizontal RecyclerView is scrolled anyway to the first item.
How could I prevent this behavior and why does it happens ?
BTW I'm writing result of this check in 'onBindViewHolder()` to the log and I can see that it is the same item.
Thanks.
RecyclerView creates new ViewHolder in order to perform animation. setItemAnimator(null) solved my problem.

How to remain at a scroll position in RecyclerView after adding items at its first index?

I am using a RecyclerView in my app, appending more items to a RecyclerView after its last item makes the RecyclerView's scroll remain at its position but when I add items before the first item, the scroll position moves up to the newly first item added. How can I maintain my scroll position after adding new items at its first index?
RecyclerView has a method yourRecyclerView.scrollToPosition(int_position_value); you just need to pass this method your position and it will scroll down or up depending on position. Now what you have to do is after appending item/items calculate the position where you want to scroll and pass it to this method.

how to use view holder if custom listview row content are dynamic?

i have create custom list view using base adapter to dynamic row
content.row content are created programmatically (check box,text view) they are include in layout.
problem to scrolling time they are very slow because not use
view holder. how can i use view holder this type of custom list view?
any solution or suggestion?
following this list..
ViewHolder is use in a list view when same view is repeated. Say there are total 6 items visible at a time in your activity. Then using viewholder pattern 6+2=8 views will be inflated at a time. one extra at the top and one extra at the bottom to give smooth scrolling effect. Now suppose scroll up operation is performed, and item at 8th position is visible, item at 0th position will be recycled and appended at the end of the list as the 9th item. if the views are not same this recycling can not be performed. check https://www.youtube.com/watch?v=wDBM6wVEO70
For your problem you can assume there is 5 maximum value possible then you can create adapter view using 10 dynamic views inside and set visibility as required.
Another optionis use LinearLayout and add each row dynamically but this won't give much optimization.

Categories

Resources