'RecyclerView: disable scroll to the end after move first item to end
If first item moved to the end of list, recyler scrolled to the end. But, if second element moved to the end -- in this case nothing happens. I want disable scrolling for first case for uniform behavior.
Solution 1:[1]
Try setup RecyclerView adapter like this:
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
if (positionStart == 0) {
recycler.getLayoutManager().scrollToPosition(0);
}
}
});
Solution 2:[2]
Best solution than I could find is override onLayoutChildren for LayoutManager
override fun onLayoutChildren(
recycler: RecyclerView.Recycler?,
state: RecyclerView.State?
) {
val state1 = onSaveInstanceState()
super.onLayoutChildren(recycler, state)
onRestoreInstanceState(state1)
}
Looks like the root of the problem in androidx.recyclerview.widget.LinearLayoutManager#updateAnchorFromChildren
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kenumir |
| Solution 2 | icebail |
