mikepenz / FastAdapter

The bullet proof, fast and easy to use adapter library, which minimizes developing time to a fraction...
https://mikepenz.dev
Apache License 2.0
3.84k stars 494 forks source link

Add new items to top of the list with header item #839

Closed hannta closed 4 years ago

hannta commented 4 years ago

I try to build list with header and footer. When the header is clicked, new items are added to top and with footer click items added to bottom. I use FastAdapterDiffUtil to set items.

My FastAdapter is setup like this:

headerAdapter = ItemAdapter()
footerAdapter = ItemAdapter()
itemsModelAdapter = ModelAdapter { ListItem(it) }
fastAdapter = FastAdapter.with(listOf(headerAdapter, itemsModelAdapter, footerAdapter))

When I set new items to itemsModelAdapter is use DiffUtil:

val listItems = items?.map { ListItem(it) }.orEmpty()
FastAdapterDiffUtil.set(itemsModelAdapter, listItems, ListItem.DiffCallback(), false)

The problem:

Sorry if this question is more related to recycler view itself than FastAdapter, but sometimes these goes pretty tightly hand in hand 😊

mikepenz commented 4 years ago

@hannta in general the question is mainly related to the RecyclerView as the adapter only serves as providing instance. e.g. provides the elements to the RecyclerView, notifies about changes in the set of data.

You could try to scroll the the according location again? e.g. use the recyclerView API to scrollTo without animation as soon as you changed the data. (this might get a bit tricky with the DiffUtil as it does things async and updates only changed elements) but it might be it already :)

hannta commented 4 years ago

Thanks @mikepenz! I tried to avoid manually setting RecyclerView scroll position, but looks like it is the only way forward.

I managed to get this somewhat working, setting scroll position via layout manager like (recyclerview.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(position, 0). Looks like it works with DiffUtil too, even it is not inserting/updating items synchronously(?). Or maybe my test list is just too small, and DiffUtil is quick enough to update the items.

mikepenz commented 4 years ago

Perhaps the RV is clever enough to handle this luckily ☺️