Android ListView - Scroll Up Update

I have a list that adds additional on-demand views that are supported by the base adapter. How to keep scroll position after update?

I know this has been set several times, but the same solution was sent each time, which I tried to call adapter.notifyDataSetChanged(); after updating the ArrayList containing the contents of the list.

How can I ensure that the scroll position is maintained?

+6
source share
5 answers

OnScrollListener in your activity class, and then use the following code:

 int currentFirstVisibleItem, currentVisibleItemCount, currentTotalItemCount; public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { this.currentFirstVisibleItem = firstVisibleItem; this.currentVisibleItemCount = visibleItemCount; this.currentTotalItemCount = totalItemCount; } public void onScrollStateChanged(AbsListView view, int scrollState) { this.currentScrollState = scrollState; this.isScrollCompleted(); } private void isScrollCompleted() { if (currentFirstVisibleItem + currentVisibleItemCount >= currentTotalItemCount) { if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) { //Do your work } } } 

If you use AsyncTask to update your data, you can include the following in your PostExecute() to maintain the scroll position:

 list.setAdapter(adapter); list.setSelectionFromTop(currentFirstVisibleItem, 0); 

Hope this helps.

+12
source

The approach I take is to call ListView.setSelection(position) to scroll to the desired position after the update.

Depending on where you are calling it from, you may need to call requestFocusFromTouch before calling setSelection to ensure that the element is positioned correctly.

+5
source

This code will help you ...

 // get position of listview int index = listView.getFirstVisiblePosition(); View v = listView.getChildAt(0); int top = (v == null) ? 0 : v.getTop(); // notify dataset changed or re-assign adapter here //re-assign the position of listview after setting the adapter again listView.setSelectionFromTop(index, top); 

Greetings :)

+3
source

You can try the following:

 //Get the top position from the first visible element int fVisible = list.getFirstVisiblePosition(); View vFirst = list.getChildAt(0); int pos = 0; if (vFirst != null) pos = vFirst.getTop(); //Restore the position list.setSelectionFromTop(fVisible, pos); 
+2
source

You can do as below. If you want to load more data with a scroll down, this will have some changes.

 int currentFirstVisibleItem, currentScrollState; int countChange = 0; public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { Log.d("Scroll", "Scrolling"); this.currentFirstVisibleItem = firstVisibleItem; countChange = 0;//during touching you can decide not to refresh by scroll } public void onScrollStateChanged(AbsListView view, int scrollState) { Log.d("Scroll", "Changed"); this.currentScrollState = scrollState; if (scrollState == SCROLL_STATE_TOUCH_SCROLL) { countChange = 0; } if (++countChange > 1) { //scroll to top and release touch change=2. Simple scroll and release touch change=1 this.isScrollCompleted(); countChange = 0; } } private void isScrollCompleted() { Log.d("Load", "Reload"); if (currentFirstVisibleItem == 0) { //Your loading go here } } 
0
source

Source: https://habr.com/ru/post/922442/


All Articles