There is a database I load from there the data in ListView .
How to make it so that only 15 items are displayed and when scrolling, another 15 items are displayed?
There is a database I load from there the data in ListView .
How to make it so that only 15 items are displayed and when scrolling, another 15 items are displayed?
You need to keep track of the number of the last visible item in the ListView and, depending on it, load the following items.
Here is an example.
To implement data uploading, create a slider:
public class EndlessScrollListener implements OnScrollListener { private int visibleThreshold = 5; private int currentPage = 0; private int previousTotal = 0; private boolean loading = true; public EndlessScrollListener() { } public EndlessScrollListener(int visibleThreshold) { this.visibleThreshold = visibleThreshold; } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (loading) { if (totalItemCount > previousTotal) { loading = false; previousTotal = totalItemCount; currentPage++; } } if (!loading &&(totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { // подгрузка данных loading = true; } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } } and install this list in your ListView :
mListView.setOnScrollListener(new EndlessScrollListener()); In fact, everything is somehow somewhat complicated here, you can make it somewhat easier, but in general the idea should be clear.
And most importantly: I don’t know exactly how elements are drawn in ListView , but when scrolling quickly in RecyclerView , elements can be drawn not one by one but two or more, therefore, if you start loading data, for example, on the penultimate element, then this download and do not start (since the last but one element and the last element can be drawn at the same time, and events when the last but one element will not be visible). Therefore, if you start the download on the Kth element, and the entire N elements, then start the download on all elements from K to N inclusive (of course, checking if the data loading was not started earlier).
Source: https://ru.stackoverflow.com/questions/597391/
All Articles