Android listview pull-down refresh pull-up automatically load more demo examples

The code download address has been updated. Because the code has not been updated for a long time, it is very outdated. We recommend that you use recyclerview to implement it.

Reference items:

https://github.com/bingoogolapple/BGARefreshLayout-Android

https://github.com/baoyongzhang/android-PullRefreshLayout

Pull down refresh, a very common feature in Android. For convenience, you can rewrite the listview to realize drop-down refresh, and add more functions of pull-up automatic loading. The design was originally based on the open source Chinese Android client source code. First look at the example diagram.

Figure 1 Figure 2

Figure 3 Figure 4

Figure 5 Figure 6

Animation effect during pull-down refresh: Figure 1 = =, figure 2 = =, figure 3 = =, figure 4 = =, figure 1.

Pull up automatically loads more effects: Figure 5

Figure 6 is a screenshot of an example demo

The rewritten listview animation effect comes from the added header and footer. Listview provides addheaderview and addfooterview methods to add header and footer. You can also define your own animation effects by modifying the XML files at the head and tail.

Implementation principle

1. Pull down refresh

Use ontouchevent to judge the gesture to change the header of listview. There are four kinds of header States, which are defined as:

None (corresponding to figure 1): initial state

Pull (corresponding to figure 2): pull-down status. At this time, the state is restored to none without refreshing

Release (corresponding to figure 3): it is also in the pull-down state, but at this moment, the refresh is executed and the state is refreshing

Refresh (corresponding to figure 4): refresh is in progress and enters the state none after refresh.

When the header switches between the four states, it not only changes the internal components, but also changes its size. Change the embodiment of internal components, such as the upward or downward arrow, the change of text prompt, and wait for the display of the circle. The change of size is actually the change of height. When none is used, the height of the header is 0. When release is used, the height of the header is determined by the degree of drop-down.

2. Load more

After the listview sliding stops, judge whether the last item of the listview has been displayed. If the display indicates that the listview has slid to the bottom, it will trigger the loading of more methods. After the method ends, change the footer according to the result.

3. Callback method

Two interfaces onrefreshlistener and onloadlistener are defined in the class to define and provide methods for loading data. The specific implementation is left to their implementation classes.

The logical complexity of this class is the judgment and switching of the four states of the header.

The following is the switching of several states

NONE ==》 PULL NONE 《== PULL ==》 RELEASE PULL 《== RELEASE ==》 REFRESHING REFRESHING ==》 NONE

The switching of release = = "pull" status in the code is not ideal. It seems that there is a problem with the recording method of ordinates. If anyone solves it, I hope to leave a message.

In order to reduce space, other codes are not posted.

Note: the outermost layer of XML defining the header must use linear layout, otherwise an error will occur.

The above is the Android listview pull-down refresh pull-up auto load more demo examples introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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