0

I have recycler view with datas that when it on the bottom with this code findLastCompletelyVisibleItemPosition() it will load new data. my init data and my load data are the same function just add change the body request after recyclerview scroll to very bottom. My issue is when it load a new datas it will scroll back to top. how do I make it in the same place and not scroll back?

I already tried to impelemnt this in load data function

val recyclerViewState =list_view_history_program.layoutManager?.onSaveInstanceState()
                if (loadMore > 0) {
                    listAdapter.notifyItemRangeChanged(loadMoreCount, listAdapter.itemCount, programNames)
                    listAdapter.notifyDataSetChanged()
                    list_view_history_program.layoutManager?.onRestoreInstanceState(recyclerViewState)
                    list_view_history_program.scrollToPosition(loadMoreCount)
                }

but still it scroll back. please help

Qube
  • 543
  • 3
  • 9
  • 23
  • have you tried https://stackoverflow.com/questions/28658579/refreshing-data-in-recyclerview-and-keeping-its-scroll-position? – Hisham Hijjawi Apr 20 '21 at 00:20
  • yes I already tried with Parcelable too. did load data function cannot be the same with load more data? – Qube Apr 20 '21 at 01:16

2 Answers2

0

You already used notifyItemRangeChanged then used notifyDataSetChanged. Remove listAdapter.notifyDataSetChanged()

Uuu Uuu
  • 1,282
  • 2
  • 10
  • 18
0

Use below code after add item in itemList array
NOTE : but you must call adapter the first time then second time use the below code

adapter.notifyItemInserted(itemList.size());
Mr. A
  • 71
  • 7