Implementation of refresh paging for Android recyclerview

Refresh paging is often used in development. Here, a simple refresh paging operation of recyclerview is implemented. The test results are shown at the end of the paper. The implementation process is referred to as follows:

Realization idea

How to get firstvisibleitemposition

In order to dynamically determine when to load data during data loading, you need to know the location of the first visible item displayed on the screen. Of course, the layout manager is used here, which is linear layoutmanager, so it is much easier to find the first visible item on the screen. Here are four methods of linear layoutmanager:

findFirstVisibleItemPosition()

Get the position of the first visible item on the screen. As long as part of the item is visible, the returned position is the position of the item.

findFirstCompletelyVisibleItemPosition()

Get the position of the first fully visible item on the screen. As long as part of the item is not visible, the returned position is the position of the next fully visible item corresponding to the item.

findLastVisibleItemPosition()

Get the position of the last visible item on the screen. As long as part of the item is visible, the returned position is the position of the item.

findLastCompletelyVisibleItemPosition()

Get the position of the last fully visible item on the screen. As long as part of the item is not visible, the returned position is the position of the last item corresponding to the item that can display a complete item.

Prepare data

Code reference

Main layout

Item layout

Adapter

Here, the recyclerview is used to load multiple layouts according to different ViewTypes. When using, create different viewholders according to different layouts, and then add data for the corresponding item according to different viewholders. Note the usage of getitemviewtype() method. The reference of adapter code is as follows:

MainActivity

Here, we mainly pay attention to the specific implementation of RV. Addonscrolllistener (New onscrolllistener())... The mainactivity code reference is as follows:

Test effect

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
分享
二维码
< <上一篇
下一篇>>