Android swiperefreshlayout drop-down refresh

Android swiperefreshlayout drop-down refresh

We all know that material design has been advocated since Android 5.0. There is a very good design swiperefreshlayout in material design. Let's take a look at its use. Since it comes from material design, the first step should be to add its library.

1. Let's add the library in build.gradle:

2. Then we'll go directly to RES / layouts / activity_ In the main.xml layout:

We can see that swiperefreshlayout is the parent layout of listview. When sliding to the boundary of listview, swiperefreshlayout will display the animation being refreshed and provide an onrefresh event for us to load data.

3. Provide data source

Here, we can directly use arrayadapter, so we can directly define string array.

4. Set adapter

It mainly implements the swiperefreshlayout.onrefreshlistener interface, then implements onrefresh to refresh the data, and then updates the data by refreshing the data source. In fact, it is very simple to use.

Let's look at the other properties of swiperefreshlayout.

setColorSchemeResources(R.color.orange,R.color.green,R.color.blue); Change the color of the load icon. In this way, swiperefreshlayout will switch between these three colors when rotating

Setenabled (false) disables the use of refresh notifications

This attribute may be used in one place, that is, when swiperefreshlayout contains multiple childviews, there is a problem of sliding event conflict. The listview can only slide up, not down. Once it is pulled down, the drop-down refresh of swiperefreshlayout will be triggered. There must be something wrong with the distribution of the incident. Normally, drop-down events should be handled by listview; When the listview is at the top, it is handled by swiperefreshlayout. The current situation is all handled by swiperefreshlayout. There are two solutions to this problem:

1. We know that this is because of the problem of sliding distribution. We can customize an improved swipelayout inherited from swipelrefreshlayout;

Create a new attrs.xml in the values folder, as follows:

Specify the ID of listview in use custom view:

Finally, all the code of my improvedswipelayout:

Another method is that we use setenabled above, which is implemented through onscrolllistener of listview. When the first visible item is 0, we will setenabled (true), otherwise vice versa.

In this way, we can solve this problem well.

Thank you for reading, hope to help you, thank you for your support to this site!

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