There are more implementation schemes for listview pull-down refresh and pull-up loading in Android

Listview is often used in combination with refresh and pull-up loading. This paper summarizes three commonly used schemes and explains them respectively.

Scheme 1: add head layout and foot layout

The Android system provides two APIs for listview: addfootview and addheadview. In this way, you can directly customize a view to refresh and pull up in the form of adding a view.

Implementation steps

1. Create a class to inherit listview: class pulltorrefreshlistview extends listview;

2. Add headview in the construction method: addheaderview (headview);

3. Gets the height of the headview. There are two methods for measuring the height of a control: getmeasuredheight and getHeight. Getmeasuredheight () can be obtained only after the onmeasure method is executed; Getheight() can get the value only after the onlayout method is executed;

4. To display and hide the headview, you can use setpadding. When you are sliding down and the first visible item is item 0, you need to set the paddingtop of the headview to display the headview.

Display: headview.setpadding (0,0);

Hide: headview.setpadding (0, - headviewheight, 0);

5. Judge the three states of pull-down refresh. When moving, when paddingtop < 0, it indicates that the headview is not fully displayed and enters the pull-down refresh state; When paddingtop > = 0 when moving, it indicates that the headview has been fully displayed and enters the released state; When the finger is raised and the current state is the refresh state released, it enters the refresh state; When the status is "refreshing", the operations of "pull-down refresh" and "release refresh" are not allowed. Add judgment in the move event. If the status is already refreshing, the pull-down operation is not processed.

6. The rotation of the pull-down arrow. The pull-down refresh is downward, and the release refresh is upward. Rotation animation is achieved through attribute animation. Clear animation when hiding arrows: IV_ arrow.clearAnimation(); If the animation effect is not hidden, it can still be seen after setting view.go.

7. When the headview is displayed, the processing when the finger is released. If the "refreshing" state is released, the headvie will be fully displayed; If it is in the "pull-down refresh" state when it is released, the headview will be completely hidden

8. Add footerview: addfooterview (footerview). When the listview is idle and the last visible item is the last data in the listview, the footview is displayed. After the footerview is displayed, the listview will not automatically slide up to display the footerview, so it needs to be set manually: setselection (getcount() - 1); The last one is selected.

9. Add callback listener. When the listview is in refresh state, onrefreshing () method will be called; Onloadmore() is called when the listview is loaded more. Notifies the control that loading is complete when loading is complete.

Specific implementation:

Scheme 2: multi style display of listview

Two methods can be implemented when setting the listview adapter: getviewtypecount () and getitemviewtype (). The former specifies the type of the item, and the latter returns the specific type. In this way, relevant styles can be designed according to different types, including pull-up loading and pull-down refresh. They are similar. Therefore, only more loading methods are given here. The specific implementation is as follows:

1. Rewrite getviewtypecount() and getitemviewtype(), including ordinary item items and loading more items, so the return value of getviewtypecount() is 2;

2. Add layouts for different types in getview():

3. Specific implementation of loading more views

Scheme 3: swiperefreshlayout is implemented

Swiperefreshlayout is not compatible with the bottom, and only the pull-down refresh function is available, and no more functions are loaded by the pull-up. At that time, as a new feature after Android 5.0, it is easy to use and can directly call the system API. The use method is also relatively simple. The specific implementation is as follows:

First declare the control and set the color:

Write a class to implement swiperefreshlayout.onrefreshlistener, and override onrefresh() method:

The above is a variety of implementation schemes for listview pull-down refresh and pull-up load in Android 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
分享
二维码
< <上一篇
下一篇>>