Android uses pulltorefresh to implement pull-down refresh and pull-up loading

Pulltorefresh is a set of pull-down refresh libraries with very good implementation. It supports:

1.ListView

2.ExpandableListView

3.GridView

4.WebView

And other common view types that need to be refreshed, and it is also very convenient to use.

Demo instance Download

After downloading, import it into eclipse and import it into your project as a library.

1、 Cut the crap and go.

1. Add the view you want to use in your layout file. For example, here I want to use an expandablelistview that supports drop-down refresh

2. Make simple settings in your activity Code:

The first line is to find the view, the last line is to add a refresh listener to it, and the middle lines are some settings I made for the expandablelistview.

In this way, you can actually pull-down refresh, but where is the code to be run during refresh, and why the pull-down will not be put away, and look down.

3. Method onrefresh() executed during pull-down refresh

Generally speaking, we will open another thread to obtain data, so a judgment will be added here. If we are already obtaining data, onrefreshcomplete(), that is, put away the drop-down; Otherwise, open a new thread to fetch data. Remember to onrefreshcomplete() after fetching data!

2、 Pull up loading

If you don't want to take the time to write a pull-up load yourself, you might as well try the pull-up effect of pulltorefresh!

Pulltorefresh itself supports pull-down refresh and pull-up refresh, so we just need to change the pull-up refresh to pull-up load.

1. Set mode

When the mode is set to mode. Both pull-down and pull-up will execute the methods in onrefresh().

Because we want to display "pull-down refresh" on the upper side of the interface and "pull-up load" on the lower side, the last three lines are to change the text in the lower part. You can feel the getloadinglayoutproxy (false, true) method for yourself.

2. How to distinguish pull-down / pull-up

Some students on the Internet use onscrolllistener to judge, which is not rigorous. I rely on whether the header or footer is visible to distinguish between pull-down and pull-up. If it is pull-down, the header must be visible; On the contrary, footer must be visible.

However, pulltorefreshexpandablelistview does not provide such an interface. Let's make minor changes to the project we introduced, which is very simple:

Find the class pulltorefreshadapterviewbase.java under the package "com. Handmark. Pulltorefresh. Library", and add two new interfaces:

That's it. Recompile this project and your own project.

In onrefresh(), use this:

It's very simple. In this way, we can YD use pulltorefresh to realize pull-down refresh and pull-up loading. Lol, I hope it can help you more or less.

=================================================================

Updated on July 1, 2014

Recent findings:

1. To realize pull-up monitoring, you only need to implement onrefreshlistener 2. At the same time, don't forget setmode (mode. Both)!

2. There is a bug in the use of pulltorefreshlistview. In your XML layout, you cannot set its visibility to go at the beginning. Otherwise, setting its visibility to visible in the code has no effect.

Finally, put a rendering

Demo: Download

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