Android Recyclerview Drop-down Refresh and pull up load more _android

Source: Internet
Author: User
Tags inheritance

Today finally a bit of time, to write: for Recyclerview implementation Drop-down refresh and pull load more. Today will be based on the previous two articles:
Recyclerview Series (1): Add header and footer for Recyclerview
Recyclerview Series (2): Add divider line for Recyclerview
Continue to talk about the implementation of some common components in Recyclerview Drop-down refresh and pull load more features.
In today's Android phone applications, almost every app has Drop-down refresh and pull load more functions, their importance is self-evident.

First of all, see the effect chart:

Pull down Refresh effect chart

Pull load more effect graph

Drop-down refresh and pull load These two features, the first feeling is that they are a combination of what is not the relationship between them, but in fact, the way to achieve is completely different. I'll take a look at their implementation with the core code.

I. Implement drop-down refresh
In Google's ANDROID.SUPPORT.V4 package, provide a swiperefreshlayout method for the implementation of the Drop-down refresh, the implementation of the process is also very simple, then we first look at what Swiperefreshlayout is what, In fact, from the name point of view, it is a refreshing layout, we look at its inheritance structure diagram:


Swiperefreshlayout Inheritance structure diagram

As you can see from the above inheritance structure, it inherits from: ViewGroup. And our common LinearLayout, GridLayout and other common layout, but also inherited from ViewGroup. So the way it's used is similar to what we usually do, we want to refresh our recyclerview, and we're going to put our Recyclerview layout files in Swipelayout.

In Swiperefleshlayout, it provides an interface: Swiperefreshlayout.onrefreshlistener, and provides an abstract method in this port: Onrefresh (), here, We probably know how it works, we just need to implement this interface in the activity, and implement the Onrefresh () method, in the Onreflesh () method, in our refresh data operation, the following directly to see the main code:

Files in the main layout: Add the following code:

<android.support.v4.widget.swiperefreshlayout
 android:id= "@+id/layout_swipe_refresh"
 android:layout _width= "Match_parent"
 android:layout_height= "match_parent" >

 < Android.support.v7.widget.RecyclerView
 android:id= "@+id/recyclerview"
 android:layout_width= "Match_ Parent "
 android:layout_height=" match_parent >
 </android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>

Is that the swiperefreshlayout contains our Recyclerview.

In the activity

Implement the following core code:

1. Create a Swiperefreshlayout object that is initialized in the OnCreate () method.
mrefreshlayout = (swiperefreshlayout) Findviewbyid (R.id.layout_swipe_refresh);
2. Add Swiperefreshlayout.onrefreshlistener event for it:

Mrefreshlayout.setonrefreshlistener (New Onrefreshlistener () {public
 void Onrefresh () {
  //I add a piece of data before the list
 mdata.add (0, "Hey, I'm" Drop-down Refresh "born out");

 After the data reload completes, the prompt data changes, and the setting is not now refreshing
 madapter.notifydatasetchanged ();
 Mrefreshlayout.setrefreshing (FALSE);
 }
);

Here, the implementation of the Drop-down refresh function, the implementation of specific adapter, divider line to join, please see my previous two articles, or look at the back of the source code
In this way, we implement the Drop-down refresh function, the following to implement the pull load more features.

Two. Pull up load more

Pull load, the main implementation is a similar paging function, can not start at the time to load all the data, if the data are many, or the network speed is slow, it takes a long time to load completed. And the idea of pull load is: I started, I will give you load 20 data around, if you want to see the data below the picture, again to load 20, batch load, so as to enhance the user experience.

In Recyclerview, another listener interface is provided: Recyclerview.onscrolllistener
We can use this interface to create more features for our pull up loading.

The

is understood directly by code, and there are more detailed parsing on the code

Package Com.study.wnw.recyclerviewrefresh;
Import Android.support.v7.widget.LinearLayoutManager;
Import Android.support.v7.widget.RecyclerView;

Import Android.util.Log; /** * Created by WNW on 16-5-26. * * Public abstract class Endlessonscrolllistener extends recyclerview.onscrolllistener{//declaration of a Linearlayoutmanager Priv

 Ate Linearlayoutmanager Mlinearlayoutmanager;
 Current page, starting with 0 private int currentpage = 0;

 The quantity of the item already loaded is private int totalitemcount;

 Primarily used to store the last totalitemcount private int previoustotal = 0;

 The number of item visible on the screen is private int visibleitemcount;

 The first private int firstvisibleitem in the visible item on the screen;

 Whether the data is being pulled private Boolean loading = true; Public Endlessonscrolllistener (Linearlayoutmanager linearlayoutmanager) {This.mlinearlayoutmanager =
 Linearlayoutmanager; @Override public void onscrolled (recyclerview recyclerview, int dx, int dy) {super.onscrolled (recyclerview, dx, dy) 

 ;
 VisibleItemCount = Recyclerview.getchildcount (); Totalitemcount = MlinearlayoutManager.getitemcount ();
 Firstvisibleitem = Mlinearlayoutmanager.findfirstvisibleitemposition ();
  if (loading) {//LOG.D ("Wnwn", "Firstvisibleitem:" +firstvisibleitem); 
  LOG.D ("Wnwn", "Totalpagecount:" +totalitemcount); LOG.D ("Wnwn", "VisibleItemCount:" + visibleitemcount); if (Totalitemcount > Previoustotal) {//Description data has been loaded over Loadin
  g = false;
  Previoustotal = Totalitemcount;
  }//Here needs to be well understood if (!loading && totalitemcount-visibleitemcount <= firstvisibleitem) {currentpage + +;
  Onloadmore (currentpage);
 Loading = true; }/** * Provides a free method to monitor this endlessonscrolllistener in the activity and implement this method */public abstract void Onloadmore (int curre

 Ntpage);}

In the above code, the need to pay attention to is understanding: VisibleItemCount, Totalitemcount, firstvisibleitem the meaning of these words, in the code to see parsing, if you do not understand, you can play log in the program, sliding screen, look at log, It's better to understand.

The listener is ready, and then we're going to add this listener to recyclerview in the activity and implement the abstract method Onloadmore () so it's OK. Look at the main code below:

Mrecyclerview.addonscrolllistener (New Endlessonscrolllistener (Mlinearlayoutmanager) {
 @Override
 public void Onloadmore (int currentpage) {
 loadmoredata ();
 }
});

The Loadmoredata () method in the above program is:

Each time the pull is loaded, 10 data data is added to the back of the Recyclerview
private void Loadmoredata () {for
 (int i =0; i < i++) {
 Mdata.add ("Hey, I'm" pulled up "born" +i);
 Madapter.notifydatasetchanged ();
 }

Here, the entire Recyclerview add Drop-down refresh and pull load function on the realization of the specific source: Please see GitHub address: Source download.

Here, our entire Recyclerview series (3) is realized, these three articles, it can do, ListView can do, and does not reflect the flexibility of recyclerview, about recyclerview flexibility, A lot of people will think of a word: waterfall flow, look at the picture below


Pictures from the network

And next article I will bring the realization of Recyclerview waterfall flow, thank you slightly.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.