Android recyclerview pull-down refresh and pull-up load more

Today, I finally have some time to write: to realize pull-down refresh and pull-up load for recyclerview. Today, we will build on the previous two articles: recyclerview series (1): adding headers and footers to recyclerview series (2): adding separators to recyclerview, and continue to talk about the implementation of some common components in recyclerview, including pull-down refresh and pull-up loading. In today's Android mobile applications, almost every app has more functions of pull-down refresh and pull-up loading, and their importance is self-evident.

Let's not say more, let's look at the renderings first:

Drop down refresh renderings

Pull up to load more renderings

The two functions of pull-down refresh and pull-up load give people the impression that they are a combination at the beginning, and whether there is any relationship between them, but in fact, the implementation method is completely different. Now I'll talk about their implementation in combination with the core code.

1、 Implement drop-down refresh in Google's android.support.v4 package, a swipererefreshlayout method is provided to implement drop-down refresh. The implementation process is also very simple. Let's first look at what swipererefreshlayout is. In fact, from the name, it is a refresh layout. Let's look at its inheritance structure diagram:

Inheritance structure diagram of swiperefreshlayout

From the above inheritance structure, we can see that it inherits from ViewGroup. And our common layouts such as LinearLayout and GridLayout also inherit from ViewGroup. Therefore, its use method is similar to our common method. If we want to refresh the drop-down to refresh our recyclerview, we need to put our recyclerview layout file into swipelayout.

In swiperefleshlayout, it also provides an interface: swiperefleshlayout. Onrefreshlistener, and an abstract method: onrefresh(). Here, we probably know how it works. We only need to implement this interface and onrefresh() method in activity. In onrefresh() method, To refresh our data, let's look at the main codes directly below:

Add the following code to the file in the main layout:

Our recyclerview is included in swiperefreshlayout

In activity

Implement the following core code:

1. Create a swiperefreshlayout object and initialize mrefreshlayout = (swiperefreshlayout) findviewbyid (r.id.layout_swipe_refresh) in oncreate() method; 2. Add swiperefreshlayout.onrefreshlistener event to it:

Here, we have realized the function of pull-down refresh. For the implementation of specific adapters and the addition of separation lines, please see my previous two articles or the attached source code. In this way, we have realized the function of pull-down refresh. Next, we will realize more functions of pull-up loading.

2、 Pull up to load more

Pull up loading mainly implements a function similar to paging. It cannot load all data at the beginning. If there is a lot of data or the network speed is slow, it will take a long time to load. The idea of pull-up loading is: at the beginning, I will load about 20 pieces of data for you. If you still want to see the following data painting, load 20 pieces again and load them in batches, so as to improve the user experience.

Next, in recyclerview, another listening interface is provided: recyclerview.onscrolllistener. We can use this interface to create more functions of pulling up and loading.

Understand it directly through the code, and there is also a more detailed analysis on the code

In the above code, you should pay attention to understanding the meaning of the words visible itemcount, totalitemcount and firstvisibleitem. Read and analyze them in the code. If you don't understand them, you can type the log in the program, slide the screen and read the log. This is a better understanding

The listener is ready. Next, we will add this listener for recyclerview in the activity and implement the abstract method onloadmore(). Here are the main codes:

The loadmoredata() method in the above program is:

Here, the function of adding pull-down refresh and pull-up load to the whole recyclerview is realized. For the specific source code, please see GitHub address: source code download

Here, part (3) of our entire recyclerview series has been implemented. Listview can do whatever it can in these three articles, which does not reflect the flexibility of recyclerview. Many people will think of a word about the flexibility of recyclerview: waterfall flow. See the figure below

Pictures from the Internet

In the next article, I will bring the implementation of recyclerview waterfall flow. Thank you.

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