Terminator of Android pulltorefreshlayout drop-down refresh control

When it comes to the pull-down refresh control, there are many online versions, and many software also have the pull-down refresh function. There is an xlistview. I've seen others use it, but I haven't seen how it is implemented. It's estimated that the name is inherited from the listview modification, but the effect looks ugly, has no scalability, and is too monotonous. After looking at the drop-down refresh of qq2014, I found it very nice. I like it. Post the following figure to see the drop-down refresh effect of QQ:

Isn't it good? Well, yes. You can see that the implementation method is different. Let's implement a drop-down refresh control today. Sometimes not only the listview needs drop-down refresh, but also expandablelistview and GridView. Because listview and GridView are subclasses of abslistview, expandablelistview is a subclass of listview, so it is also a subclass of abslistview. So my idea is to customize a pull-down management layout common to all subclasses of abslistview, called pulltorefreshlayout. If you need a GridView, you just need to replace the listview with a GridView in the layout file. The same is true for expandablelistview. You don't need to inherit any GridView or listview.

As shown in the figure above, the main work is to define the large black layout. The red part is the visible part when it is not pulled down. It can be any subclass of abslistview (GridView, listview, expandablelistview, etc.). Actually, I've already written it. Let's take a look at the renderings:

Normal pulling method:

OCD Rafah:

Listview above and GridView below

Let's take another look at the drop-down refresh effect of expandablelistview:

It can be seen that both click events and long press events can be triggered normally without false triggering. When using expandablelistview, you should pay attention to prohibit automatic scrolling during expansion, otherwise there will be a bug. The demo source code download will be provided later, which can be modified according to your own needs.

The implementation of pulltorefreshlayout is explained below. Before pasting the complete source code, understand the general idea of the whole class:

You can see that the dispatchtouchevent of ViewGroup is copied here, so that you can control the event distribution. If you don't know this method, you can take a look at the full analysis of Android event distribution and view event listener. The reason why we want to control event distribution is that we cannot know whether the finger down will slide up or pull down on the abslistview, so the down event will be distributed to the abslistview, but it depends on the situation when moving. Because we don't want the abslistview to slide while pulling down, we don't distribute the move event when pulling down, but this problem comes again, Abslistview has received the down event. If the move event is not distributed to it at this time, it will trigger the long press event or click event. Therefore, it is also necessary to clear the callback in the abslistview message list. Onlayout is used to rearrange the position of the drop-down header and abslistview, which is not difficult to understand.

After understanding the general idea, take a look at the complete source code of pulltorefreshlayout~

The comments in the code have been written clearly. Now that pulltorefreshlayout has been written, let's use this layout to implement pull-down refresh~

First, write an onrefreshlistener interface to call back the refresh operation:

For a refresh operation method, let the activity implement this interface later, and then the refresh operation can be performed in the activity.

Take a look at the layout of mainactivity:

Pulltorrefreshlayout can contain only two child controls: refresh_ Head and content_ view。 Take a look at refresh_ Layout of head:

You can modify refresh as needed_ The layout of the head is then processed in pulltorrefreshlayout, but the ID of the relevant view should be synchronized with that used in pulltorrefreshlayout!

Next is the code of mainactivity:

If it is judged in mainactivity that contentview is a listview, footerview is added to the listview to realize more functions of click loading. This is just a demo to demonstrate the use of pulltorefreshlayout, which can be modified by reference. I have written the initialization methods of listview, GridView and expandablelistview in it. Call them according to which you use. Then this is the listview drop-down refresh and load more. What if I want the GridView to have a drop-down refresh function? Then change the layout of mainactivity to this:

If it is expandablelistview, change the layout to this:

What about? It's simple, isn't it? Easy to use, no need to inherit and modify.

This article has been sorted into "Android pull-down refresh pull-up loading effect", welcome to study and study.

I hope this article is helpful for you to learn Android pull-down refresh control.

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