Android hands-on tutorial 43rd pull load and drop-Pull refresh _android

Source: Internet
Author: User

ListView Drop-down Refresh is very common, many open source framework can do this, of course, can also be achieved. This case is based on Xlistview.

Layout:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" 
 xmlns:tools= "http:// Schemas.android.com/tools " 
 android:layout_width=" match_parent " 
 android:layout_height=" Match_parent " > 
 
 <me.maxwin.view.xlistview 
  android:id= "@+id/xlv" 
  android:layout_width= "Match_parent" 
  android:layout_height= "Match_parent"/> 
 

Code for the main activity:

Package Com.itydl.xlistviewdemo; 
Import Java.text.SimpleDateFormat; 
Import java.util.ArrayList; 
Import Java.util.Date; 
 
Import java.util.List; 
Import Me.maxwin.view.XListView; 
Import Me.maxwin.view.XListView.IXListViewListener; 
Import android.app.Activity; 
Import Android.graphics.Color; 
Import Android.os.Bundle; 
Import Android.os.Handler; 
Import Android.os.Message; 
Import Android.os.SystemClock; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.BaseAdapter; 
 
Import Android.widget.TextView; 
 public class Mainactivity extends activity {private Xlistview Mxlistview; 
 list<string> datas = new arraylist<string> (); 
 Private Myadapter adapter; Private Boolean isloadmore;//whether the markup for more data is being loaded private long pretime;//the current system time millisecond value that was refreshed on the previous refresh private Handler Handler = new Han 
    Dler () {public void Handlemessage (Android.os.Message msg) {if (Isloadmore) {//message is a pull load more initdatas (); 
    Isloadmore = false; Refresh complete, turn off pull load effectFruit Mxlistview.stoploadmore (); 
    }else{//message is Drop-down refresh Datas.clear (); 
    Initdatas (); 
   Refresh finished, turn off the Drop-down refresh Effect Mxlistview.stoprefresh (); 
  }//Refresh ListView adapter.notifydatasetchanged (); 
 }; 
 
 }; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.activity_main); 
  Initdatas (); 
  Mxlistview = (Xlistview) Findviewbyid (R.ID.XLV); 
  Set Adapter to ListView adapter = new Myadapter (); 
  Mxlistview.setadapter (adapter); 
 Initlistener (); 
   
  private void Initlistener () {//settings can be pulled down to refresh, the default is True Mxlistview.setpullrefreshenable (true); 
   
  The settings can be pulled up to load, the default is False mxlistview.setpullloadenable (true); Mxlistview.setxlistviewlistener (New Ixlistviewlistener () {@Override public void Onrefresh () {//dropdown refresh, solid 
    Inter-send handler//Send an empty message, delay two seconds after informing refresh Data handler.sendmessagedelayed (Message.obtain (), 2000); Add the last Refresh time: if (pretime!= 0) {Mxlistview.setRefreshtime (RefreshData (pretime)); 
   } pretime = System.currenttimemillis (); Private String RefreshData (long pretime) {return new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (New Date 
   (Pretime)); 
    The @Override public void Onloadmore () {//tag is loading more, sending handler Isloadmore = true; 
   Handler.sendmessagedelayed (Message.obtain (), 2000); 
 } 
  });  /** Analog network data/private void Initdatas () {//The actual development should be child thread execution for (int i = 0; i < i++) {Datas.add ("I am the first" + 
  i + "strip data"); 
    } private class Myadapter extends baseadapter{@Override public int GetCount () {if (datas!= null) { 
   return Datas.size (); 
  return 0; 
   @Override public Object getitem (int position) {if (datas!= null) {return datas.get (position); 
  return null; 
  @Override public long getitemid (int position) {return position; @Override public View getview (int position, View Convertview, VIewgroup parent) {//Simulate item is TextView TextView TextView = new TextView (mainactivity.this); 
   Textview.settext (GetItem (position) + ""); 
   Textview.settextcolor (Color.green); 
  return textView;  } 
 } 
}

Just a few lines of code have done the main function, run as follows:

Like friends can pay attention to me Oh, this column will not regularly update the simple and interesting Android small text

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.