How to implement drop-down refresh in Android Development

Because the recent development involves reading data from the network, it is natural that the function of pull-down refresh is indispensable. The search method is generally to customize the listview or recyclerview to rewrite the ontouch or onscroll method to realize gesture monitoring, then play the animation, and finally refresh the interface

Today, I'm talking about a drop-down refresh layout officially provided by Google. It's called swiperefreshlayout. When I found this layout, I was really overjoyed. Let's record how to use it.

Let's put the renderings here. First, this is the small circle below

First, we need to set the layout outside the controls we need to refresh. Here is recyclerview

When we need to show or hide this small animation, we need to call the following method public void setRefreshing (Boolean refreshing), but if we want to show this loaded animation at the very beginning, we can not directly introduce true into this method and then call it. Because we can see in the source code that a variable named originaloffsettop in swiperefreshlayout is not initialized, the animation may not be displayed. So how should it be displayed? Use the following method

Then, we need to add a monitor after the animation

When the data is loaded, hide the animation, which is very simple refreshlayout. Setrefreshing (false); Here, in fact, it is basically all used. This thing is really convenient, but there are no problems. If you don't pay attention, you will still encounter some problems. In many codes on the Internet, many developers like to empty the data set before loading data from the network, that is, call data. Clear() in the onrefresh method, but there will be a problem. If we slide the interface up while loading, the app will crash directly. Look at the figure

In fact, this problem is also very simple, because if we empty the data, but the list is not refreshed, if the sliding occurs, we will access the data in the data and directly cross the boundary. Therefore, the correct approach should be to load data online first. When the data is loaded, empty the collection, refill and refresh.

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