Android Development ListView List Refresh and load more implementations _android

Source: Internet
Author: User
Tags addall gety sin string format

This example describes the Android development ListView list Refresh and load more implementations. Share to everyone for your reference. Specifically as follows:

Up and down pull implementation refreshes and loads more listview as follows:

Package com.sin.android.ui; 
Import Android.content.Context; 
Import Android.util.AttributeSet; 
Import android.view.Gravity; 
Import android.view.MotionEvent; 
Import Android.view.View; 
Import Android.widget.AbsListView; 
Import Android.widget.AbsListView.OnScrollListener; 
Import Android.widget.LinearLayout; 
Import Android.widget.ListView; 
Import Android.widget.ProgressBar; 
Import Android.widget.TextView; /** * Dynamic refresh and load data ListView * @author Robintang * * */public class Dynamiclistview extends ListView implements Onscrol  
   Llistener {/** * listener * Monitor Control refresh or load more events * All entry events will have an offset, that is, position should be minus 1 is the entry in your adapter * @author Robintang  * * */public interface Dynamiclistviewlistener {/** * * * * @param dynamiclistview * @param When Isrefresh is true, it represents a refresh, which, when false, represents a load of more * @return true: Refresh or load more actions complete, refresh or load more animations automatically disappear false: Refresh or load more for completion, need to be completed in data loading To invoke the control's Donerefresh () or Donemore () method/public Boolean Onrefreshormore (Dynamiclistview Dynamiclistview, Boolean Isrefresh); /** * State control (Statusview, list header and bottom) State enumeration * @author Robintang */enum Refreshstatus {none, Norm Al, Willrefresh, refreshing}/** * State control * @author Robintang * * * * */class Statusview extends 
    Layout {public int height; 
    public int width; 
    Private ProgressBar ProgressBar = null; 
    Private TextView TextView = null; 
    Private Refreshstatus refreshstatus = Refreshstatus.none; 
    Private String normalstring = "Drop-down Refresh"; 
    Private String willrefreshstring = "Release Refresh"; 
    Private String refreshingstring = "refreshing"; 
      Public Statusview (context, AttributeSet attrs) {Super (context, attrs); 
    Initthis (context); 
      Public Statusview {Super (context); 
    Initthis (context); 
      private void Initthis (context context) {this.setorientation (linearlayout.horizontal); This.setgravity (Gravity.center_horizontal | GRavity. 
      center_vertical); 
      ProgressBar = new ProgressBar (context); 
      Progressbar.setlayoutparams (New Linearlayout.layoutparams (30, 30)); 
      TextView = new TextView (context); 
      Textview.setpadding (5, 0, 0, 0); 
      This.addview (ProgressBar); 
      This.addview (TextView); 
      int w = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); 
      int h = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); 
      This.measure (W, h); 
      Height = this.getmeasuredheight (); 
      width = This.getmeasuredwidth (); 
    This.setrefreshstatus (Refreshstatus.normal); 
    Public Refreshstatus Getrefreshstatus () {return refreshstatus; 
        public void Setrefreshstatus (Refreshstatus refreshstatus) {if (this.refreshstatus!= refreshstatus) { 
        This.refreshstatus = Refreshstatus; 
        if (Refreshstatus = = refreshstatus.refreshing) {this.progressBar.setVisibility (view.visible); 
       } else{this.progressBar.setVisibility (View.gone); 
        } refreshstatusstring (); 
      This.invalidate (); } private void Refreshstatusstring () {switch (refreshstatus) {case NORMAL:TEXTVIEW.S 
        Ettext (normalstring); 
        Progressbar.setprogress (0); 
      Break 
        Case Willrefresh:textView.setText (willrefreshstring); 
      Break 
        Case Refreshing:textView.setText (refreshingstring); 
      Break 
      Default:break; 
     }/** * Set state String * @param normalstring Normal String * @param a string that is refreshed (or loaded) after the release of Willrefreshstring * @param refreshingstring is refreshing (or loading) the string */public void setstatusstrings (string normalstring, String willrefres 
      Hstring, String refreshingstring) {this.normalstring = normalstring; 
      this.willrefreshstring = willrefreshstring; 
      this.refreshingstring = refreshingstring; This.refreshstatusstring (); 
  } private Statusview Refreshview; 
  Private Statusview Moreview; 
  private int itemflag =-1; 
  Private Boolean isrecorded = false; 
  private int DownY =-1; 
  Private final float Mintimestorefresh = 1.5f; 
  Private final static int item_flag_first = 1; 
  Private final static int item_flag_none = 0; 
  Private final static int item_flag_last =-1; 
  Two listeners private Dynamiclistviewlistener Onrefreshlistener; 
  Private Dynamiclistviewlistener Onmorelistener; 
  Whether to automatically load more private Boolean Domorewhenbottom = False when scrolling to the lower end; 
    Public Dynamiclistview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); 
  Initthis (context); 
    Public Dynamiclistview (context, AttributeSet attrs) {Super (context, attrs); 
  Initthis (context); 
    Public Dynamiclistview {Super (context); 
  Initthis (context); } private void Initthis {Refreshview = new Statusview (COntext); 
    Moreview = new Statusview (context); 
    Refreshview.setstatusstrings ("Continue drop-down Refresh Data ...", "Refresh data after release ...", "refreshing data ..."); 
    Moreview.setstatusstrings ("Continue pull loading data ...", "Load Data ...", "Loading Data ..."); 
    This.addheaderview (Refreshview, NULL, FALSE); 
    This.addfooterview (Moreview, NULL, FALSE); 
    This.setonscrolllistener (this); 
    Donerefresh (); 
  Donemore (); 
  }//Listener operation Public Dynamiclistviewlistener Getonrefreshlistener () {return onrefreshlistener; } public void Setonrefreshlistener (Dynamiclistviewlistener onrefreshlistener) {This.onrefreshlistener = OnRefresh 
  Listener; 
  Public Dynamiclistviewlistener Getonmorelistener () {return onmorelistener; 
  public void Setonmorelistener (Dynamiclistviewlistener onmorelistener) {this.onmorelistener = Onmorelistener; 
  //Set public boolean Isdomorewhenbottom () {return domorewhenbottom; } public void Setdomorewhenbottom (Boolean domorewhenbottom) {THIS.DOMOREWHEnbottom = Domorewhenbottom; @Override public void Onscroll (abslistview l, int t, int oldl, int count) {//log ("%d%d%d", T, OLDL, Count 
    ); 
    if (t = = 0) Itemflag = Item_flag_first; 
      else if ((t + oldl) = = count) {Itemflag = Item_flag_last; if (domorewhenbottom && onmorelistener!= null && moreview.getrefreshstatus ()!= 
      refreshstatus.refreshing) {domore (); 
} else {itemflag = Item_flag_none; 
    isrecorded = false; @Override public void onscrollstatechanged (Abslistview arg0, int arg1) {} @Override public boolean Ontouchevent (motionevent ev) {switch (ev.getaction ()) {case MotionEvent.ACTION_DOWN:if (isrecorded = = False && (Itemflag = = Item_flag_first && onrefreshlistener!= null && Refreshview.getrefreshstatus () = = Refreshstatus.normal | | Itemflag = = Item_flag_last && onmorelistener!= null && MorevieW.getrefreshstatus () = = Refreshstatus.normal)) {DownY = (int) ev.gety (0); 
Isrecorded = true; 
      Log ("Press, Record:%d flag:%d", DownY, Itemflag); 
    } break; 
      Case MOTIONEVENT.ACTION_UP: {isrecorded = false; if (Onrefreshlistener!= null && refreshview.getrefreshstatus () = = Refreshstatus.willrefresh) {Dorefresh 
      (); else if (refreshview.getrefreshstatus () = = Refreshstatus.normal) {refreshview.setpadding (0,-1 * refreshView.h 
      Eight, 0, 0); } if (Onmorelistener!= null && moreview.getrefreshstatus () = = Refreshstatus.willrefresh) {Domore 
      (); else if (moreview.getrefreshstatus () = = Refreshstatus.normal) {moreview.setpadding (0, 0, 0,-1 * Moreview.heig 
      HT); 
    } break; Case Motionevent.action_move: {if (isrecorded = = False && (Itemflag = = Item_flag_first && o Nrefreshlistener!= null && Refreshview.getrefreshStatus () = = Refreshstatus.normal | | Itemflag = = Item_flag_last && onmorelistener!= null && moreview.getrefreshstatus () = = 
        Refreshstatus.normal)) {DownY = (int) ev.gety (0); 
Isrecorded = true; 
      Log ("Press, Record:%d flag:%d", DownY, Itemflag); 
        else if (isrecorded) {int nowy = (int) ev.gety (0); 
        int offset = Nowy-downy; 
          if (Offset > 0 && itemflag = item_flag_first) {//dropdown setselection (0); if (offset >= (Mintimestorefresh * refreshview.height)) {Refreshview.setrefreshstatus (Refreshstatus.willr 
          Efresh); 
          else {refreshview.setrefreshstatus (refreshstatus.normal); 
        Refreshview.setpadding (0,-1 * (Refreshview.height-offset), 0, 0); 
          else if (Itemflag = = Item_flag_last) {//Oberra setselection (This.getcount ()); if (offset <=-1 * (Mintimestorefresh * Moreview. Height)) {moreview.setrefreshstatus (Refreshstatus.willrefresh);
          else {moreview.setrefreshstatus (refreshstatus.normal);
        Moreview.setpadding (0, 0, 0,-1 * (Moreview.height + offset)); 
      }//Log ("Offset:%d", offset); 
    } break; 
    } Default:break; 
  return super.ontouchevent (EV); 
    /** * Start refreshing/private void Dorefresh () {//log ("Start refresh"); 
    Refreshview.setrefreshstatus (refreshstatus.refreshing); 
    Refreshview.setpadding (0, 0, 0, 0); 
  if (Onrefreshlistener.onrefreshormore (this, true)) Donerefresh (); 
    /** * Start loading more * * private void Domore () {//log ("load more"); 
    Moreview.setrefreshstatus (refreshstatus.refreshing); 
    Moreview.setpadding (0, 0, 0, 0); 
  if (Onmorelistener.onrefreshormore (this, false)) Donemore (); 
    Called after the/** * Refresh is completed, the animation for canceling the refresh/public void Donerefresh () {//log ("Refresh complete!"); RefreshvIew.setrefreshstatus (Refreshstatus.normal); 
  Refreshview.setpadding (0,-1 * refreshview.height, 0, 0); 
    /** * Load more completed after calling to cancel loading more animations/public void Donemore () {//log ("Load complete!"); 
    Moreview.setrefreshstatus (Refreshstatus.normal); 
  Moreview.setpadding (0, 0, 0,-1 * moreview.height); /** * Get refreshed Status * @return General will refresh refresh complete/public refreshstatus Getrefreshstatus () {return Refreshvi 
  Ew.getrefreshstatus (); /** * Get load more state * @return typically will load completion of load/public refreshstatus Getmorestatus () {return MOREVIEW.G 
  Etrefreshstatus (); 
}//private void log (Object obj) {//log ("%s", obj.tostring ()); }///private void log (String format, Object ... args) {//LOG.I ("Dynamiclistview", String.Format (format, args)) 
; 

 // } 
}

Use Example:

Package com.sin.android.ui; 
Import java.util.ArrayList; 
Import Java.util.Date; 
Import Android.annotation.SuppressLint; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.os.Handler; 
Import Android.os.Message; 
Import Android.util.Log; 
Import Android.view.View; 
Import Android.widget.AdapterView; 
Import Android.widget.AdapterView.OnItemClickListener; 
Import Android.widget.ArrayAdapter; 
Import Android.widget.Toast; 
Import Com.sin.android.ui.DynamicListView.DynamicListViewListener; @SuppressLint ("Handlerleak") public class Mainactivity extends activity implements Dynamiclistviewlistener {DYNAMICLI 
  Stview ListView; 
  Arraylist<string> data; 
  Arrayadapter<string> adapter; Used to refresh control state Handler Handler = new Handler () {@Override public void Handlemessage (msg) {if (M 
        Sg.what = = 0) {adapter.notifydatasetchanged (); 
        Listview.donerefresh (); Toast.maketext (Mainactivity.this, "New Load" +msg.arg1+ "Piece of data!", Toast.length_long). Show (); 
        else if (Msg.what = 1) {adapter.notifydatasetchanged (); 
      Listview.donemore (); 
      else {super.handlemessage (msg); 
  } 
    } 
  }; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    ListView = new Dynamiclistview (this); 
    Setcontentview (ListView); 
    data = new arraylist<string> (); 
    for (int i = 1; i < ++i) {Data.add ("raw data" + i); } adapter = new Arrayadapter<string> (this, Android. 
    R.layout.simple_expandable_list_item_1, data); 
    Listview.setadapter (adapter); Listview.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (adapterview&lt ;? 
      > arg0, View arg1, int arg2, long arg3) {log.i ("", Data.get (arg2-1)); 
    } 
    });  Listview.setdomorewhenbottom (FALSE); 
    Scroll to the lower end when you do not load more listview.setonrefreshlistener (this); LisTview.setonmorelistener (this);  @Override public boolean Onrefreshormore (Dynamiclistview Dynamiclistview, Boolean Isrefresh) {if (Isrefresh) {New Thread (new Runnable () {@Override public void run () {//Refresh Arraylis 
          t<string> temp = new arraylist<string> (); 
            for (int i = 0; i < 3; ++i) {temp.add (0, New Date (). toLocaleString ()); 
            try {thread.sleep (1000); 
            catch (Interruptedexception e) {e.printstacktrace (); 
          } synchronized (data) {data.addall (0, temp); 
          } message = new message (); 
          Message.what = 0; 
          MESSAGE.ARG1 = Temp.size (); 
        Handler.sendmessage (message); 
    }). Start (); 
          else {new Thread (new Runnable () {@Override public void run () {//Load more Arraylist< 
          string> temp = new arraylist<string> (); 
            for (int i = 0; i < 3; ++i) {Temp.add (New Date (). toLocaleString ()); 
            try {thread.sleep (1000); 
            catch (Interruptedexception e) {e.printstacktrace (); 
          } synchronized (data) {Data.addall (temp); 
        } handler.sendemptymessage (1); 
    }). Start (); 
  return false; 

 } 
}

Run the screenshot as follows:

I hope this article will help you with your Android program.

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.