Android custom Drop-down Refresh pull load _android

Source: Internet
Author: User
Tags int size

The example of this article for everyone to share the Android custom pull down to refresh the concrete implementation steps to load, for your reference, the specific contents are as follows

The way to achieve this is Swiperefreshlayout + recyclerview viewtype

First look at the effect:

The general idea:

Layout file

<android.support.v4.widget.swiperefreshlayout
    android:layout_margintop= "? attr/actionbarsize"
    Android:id= "@+id/one_refresh"
    android:layout_width= "match_parent"
    android:layout_height= "Match_parent" >

    <android.support.v7.widget.recyclerview
      android:id= "@+id/one_recyclerview"
      android:layout_ Width= "Match_parent"
      android:layout_height= "match_parent"
      >

    </ Android.support.v7.widget.recyclerview>

  </android.support.v4.widget.SwipeRefreshLayout>

The realization idea of the Drop-down refresh

Model for testing

public class Testmodel {
  private String mtitle;
  Private String Mdesc;
  Private String mtime;

  Public Testmodel (String mtitle, String Mdesc, String mtime) {
    this.mtitle = mtitle;
    This.mdesc = Mdesc;
    This.mtime = Mtime;
  }
  //... A bunch of getxxx, setxxx method
  //equals must write, when adding data to judge
   @Override public
  boolean equals (Object o) {
    Testmodel Model = (Testmodel) o;
    if (!mtitle.equals (Model.getmtitle ())) {return
      false;
    } else if (!mdesc.equals (Model.getmdesc ())) {
      return false;
    } else if (!mtime.equals (Model.getmtitle ()) {return
      false;
    }
    return true;
  }

Simulate code to get network data

Private class GetData {
    int size = 0;
    int max = 25; The maximum value of the data is public

    void Setstart (int size) {
      this.size = size;
    }
    Gets the specified size List based on size, not exceeding Max public
    list<testmodel> initdata (int size) {
      list<testmodel> Mdatas = new arraylist<> ();
      Testmodel model = NULL;
      for (int i = START I < (size + start) > Max Max: (size + start); i++) {
        model = new Testmodel ("Title" + I , "Desc" + I, "Today 11:30");
        Mdatas.add (model);
      }
      start = = size;
      Return Mdatas

    }
  }

Data acquisition and notification of initialization Recyclerview

public void InitData () {
    if (GetData = = null) {
      getData = new GetData ();
    }
    Mlists = getdata.initdata (size); Gets the number of item mhandler.sendemptymessage (REFRESH) that is displayed by default
    ;//Notify Handler update
  }

Code for processing the first display of data and subsequent refresh operations in handler

if (msg.what = = REFRESH) {
if (Madapter = = null) {
madapter = new Oneadapter (mcontext);
Madapter.setmdatas (mlists);//Set data
//... The setting of the adapter is omitted here first to avoid confusing
Mrecyclerview.setlayoutmanager (new Linearlayoutmanager (Mcontext));
Mrecyclerview.setadapter (Madapter);
    } else {
     Madapter.setmdatas (mlists);
     Madapter.cleadncount ();
     Madapter.notifydatasetchanged ();
   }
  Initrefresh (); To determine if Refreshlayout is refreshing, cancel the refresh operation. It's not code. It's a mess.

Refresh events for Refreshlayout

Mrefresh.setonrefreshlistener (New Swiperefreshlayout.onrefreshlistener () {
   @Override public
   Void Onrefresh () {
     new Thread (mrunnable). Start ();//runnable called InitData () method;
   }
   );

The refresh operation can be responded to at this time, as is the case with the usual refreshlayout operation.

Implementation of the pull refresh (mainly in the adapter, only one activity needs to update the data source when more load is needed)

Layout of normal content

<?xml version= "1.0" encoding= "Utf-8"?> <android.support.v7.widget.cardview xmlns:android= "http://" Schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_ Parent "android:layout_height=" wrap_content "android:layout_margin=" 5DP "android:orientation=" vertical "Tools:cont Ext= ".
    Mainactivity "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Match_parent " android:orientation= "Horizontal" > <imageview android:scaletype= "centerinside" android:id= "@+id/ite" M_head "android:layout_width=" 70DP "android:layout_height=" 70DP "android:layout_gravity=" center "a Ndroid:layout_margin= "5DP" android:src= "@mipmap/ic_launcher"/> <linearlayout android:layout_width=
      "0DP" android:layout_height= "match_parent" android:layout_marginleft= "5DP" android:layout_weight= "1"

android:orientation= "Vertical" >      <textview android:id= "@+id/item_title" android:layout_width= "Match_parent" android:layout _height= "Wrap_content" android:layout_gravity= "center_vertical" android:layout_weight= "1" android: Gravity= "center_vertical" android:text= "Title" android:textsize= "20sp"/> <textview A
        Ndroid:id= "@+id/item_desc" android:layout_width= "match_parent" android:layout_height= "Wrap_content"
        Android:layout_gravity= "Center_vertical" android:layout_weight= "1" android:gravity= "center_vertical"
        android:text= "Desc" android:textsize= "16sp"/> <textview android:id= "@+id/item_time" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_gravity= "cent" Er_vertical|right "android:layout_marginright=" 14DP "android:layout_weight=" 1 "android:gravity=" ce
   Nter_vertical|right "     android:text= "Time" android:textsize= "20sp"/> </LinearLayout> </LinearLayout> </an

 Droid.support.v7.widget.cardview>

Load more content layouts (show ProgressBar by default, no more icons to hide)

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" 40DP "android:orientation=" Horizontal "> &L T;imageview android:id= "@+id/load_image" android:layout_width= wrap_content "android:layout_height=" Wrap_conte
    NT "android:layout_gravity=" Center_vertical|right "android:layout_weight=" 1 "android:scaletype=" CenterInside " android:src= "@mipmap/ic_launcher android:visibility=" Gone "/> <progressbar android:id=" @+id/load_pro Gress "style=" @style/widget.appcompat.progressbar "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:layout_gravity=" Center_vertical|right "android:layout_weight=" 1 "/> <TextView A Ndroid:id= "@+id/load_tv" android:layout_width= "wrap_content" android:layout_height= "Match_parent" Android:layo ut_gravity= "Center" android:layout_weight= "1" android:gravity= "Center_vertical|left" android:text= is loading more ... "android:textcolor=" @color/co
 Lorblank "android:textsize=" 20sp "/> </LinearLayout>

ItemCount (because we want to display the information at the end, the total number of items should be plus 1, but it's also a partial case:

@Override public
  int GetItemCount () {
    if (mdatas.size () > Minshowload) {//current item can display the screen full return

      Mdatas.size () + 1; The default display load or no more
    } return
    mdatas.size ()/////////////If full is not displayed, load and no more
  }

Getviewtype (Show different types depending on location)

@Override public
  int getitemviewtype (int position) {
    if (position = = Mdatas.size ()) {return
      Viewtype_ LOAD; Last display load Information
    } return
    viewtype_content;//otherwise show normal layout
  }

Viewholder of normal content

Content layout
  Private class Contentviewholder extends Recyclerview.viewholder {
    private TextView mtitle;
    Private TextView Mdesc;
    Private TextView mtime;
    Private ImageView Mhead;
    Private View Itemview;

    Public Contentviewholder (View itemview) {
      super (Itemview);
      This.itemview = Itemview;
      Mtitle = (TextView) Itemview.findviewbyid (r.id.item_title);
      Mdesc = (TextView) Itemview.findviewbyid (R.ID.ITEM_DESC);
      Mtime = (TextView) Itemview.findviewbyid (r.id.item_time);
      Mhead = (ImageView) Itemview.findviewbyid (r.id.item_head);
    }
  

Viewholder to load Information

Load more layouts  (for display being loaded and no more
  private class Loadmoreviewholder extends Recyclerview.viewholder {
    private ImageView Mimage;
    Private ProgressBar mprogress;
    Private TextView mmsg;

    Public Loadmoreviewholder (View itemview) {
      super (Itemview);
      Mimage = (ImageView) Itemview.findviewbyid (r.id.load_image);
      Mprogress = (ProgressBar) Itemview.findviewbyid (r.id.load_progress);
      Mmsg = (TextView) Itemview.findviewbyid (R.ID.LOAD_TV);
    }
  

Initializes different viewholder in the Oncreateviewholder

  @Override public
  Recyclerview.viewholder Oncreateviewholder (viewgroup parent, int viewtype) {
    View Itemview = null;
    Switch (viewtype) {case
      1:
        Itemview = Layoutinflater.from (Mcontext). Inflate (r.layout.load_layout, parent, FALSE);
        return new Loadmoreviewholder (Itemview);
      Case 2:
        Itemview = Layoutinflater.from (Mcontext). Inflate (R.layout.item_test, parent, false);
        return new Contentviewholder (Itemview);
    }
    return null;
  }

Defines a callback that notifies the activity to update data when the display is loaded

Public interface Onloadmorelistener {
    void Loadmore ();
  }
  Global variable
private onloadmorelistener onloadmorelistener;

Onbindviewholder (data display for different situations)

@Override public void Onbindviewholder (recyclerview.viewholder holder, int position) {if (Holder instanceof Conten
      Tviewholder) {Testmodel model = Mdatas.get (position);
      ((contentviewholder) holder). Mtitle.settext (Model.getmtitle ());
      ((contentviewholder) holder). Mdesc.settext (Model.getmdesc ());
    ((contentviewholder) holder). Mtime.settext (Model.getmtime ()); else if (holder instanceof Loadmoreviewholder) {if (Mdatas.size () < Itemscount) {//No More (Loadmorevie
        Wholder) holder). Mmsg.settext ("no More ~ ~ ~ ~");
        ((loadmoreviewholder) holder). Mprogress.setvisibility (View.gone);
      ((loadmoreviewholder) holder). Mimage.setvisibility (view.visible);
        else {onloadmorelistener.loadmore ();
        ((loadmoreviewholder) holder). Mmsg.settext ("Loading more ...");
        ((loadmoreviewholder) holder). Mprogress.setvisibility (view.visible);
      ((loadmoreviewholder) holder). Mimage.setvisibility (View.gone);

 }
    }
  }

Load more callbacks for use in activity

Madapter.setonloadmorelistener (New Oneadapter.onloadmorelistener () {
    @Override public
    void Loadmore () {
   //Add data to the data source/
   /Call Adapter AddData method
   //Update adapter display
  }
}

This drop-down refresh on the pull load is complete, demo address: Swipetorefreshtest.

The above is the Android custom Drop-down to refresh the full load of the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

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.