Android code realizes infinite scrolling of adapterviews and recyclerview

A common feature of applications is to automatically load more content when the user is active, which is realized by sending data requests when the user slides to trigger a certain threshold. The same is: the effect of information sliding needs to define the last visible item in the list and some types of thresholds, so as to start grabbing data before the last item arrives and realize infinite scrolling. The important point of realizing the phenomenon of infinite scrolling is that the data is obtained before the user slides to the lowest end, so a threshold needs to be added to help realize the expectation of obtaining data.

Implementation using listview and GridView

Each AdapterView, such as listview and GridView, will trigger onscrolllistener when the user starts scrolling. Using this system, we can define a basic endlesscrolllistener and support the use in most cases by creating classes that inherit onscrolllistener.

Note that this is an abstract class. In order to use these, we must inherit this basic class and implement the onloadmore () method to actually obtain data. We define an anonymous class in an activity to inherit endlesscrolllistener and connect it to AdapterView

Now when the user slides and triggers the threshold, the onloadmore () method is automatically triggered, and the listener gives access to the number of pages and the total amount of data.

Realize infinite sliding of recyclerview

We can use the same method to define an interface endlessrecyclerviewscrolllistener, and then define a method of onloadmore() to implement it. Since layoutmanager is responsible for item generation and sliding management, we need an instance of layoutmanager to collect the necessary information. The following steps are required to realize the necessary paging: 1) directly copy endlesrecyclerviewscrolllistener.java 2) call addonscrolllistener (...) to realize unlimited paging in recyclerview, Pass the instance of endlesrecyclerviewscrolllistener to implement the onloadmore method to decide when to load new data. 3). In the onloadmore method, you can get more items by sending a network request or loading from the source data.

EndlessRecyclerView

Note: 1. For listview, make sure to put the step of binding listener in oncreate() method. 2. For reliable paging, make sure to clean up the data in the adapter before adding new data to the list. For recyclerview, more detailed updates are recommended when notifying the adapter. 3. For recyclerview, ensure that the adapter content is updated quickly when clearing the data in the list, so that a new onscroll event can be triggered to reset itself

Show progress bar

To show the progress bar at the bottom, prove that the listview is loading. We can set it in the adapter. We can define two types: progress bar type or text indicating that the bottom line has been reached. Refer to: http://guides.codepath.com/android/Endless-Scrolling-with-AdapterViews-and-RecyclerView

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>