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

Source: Internet
Author: User

There are a lot of analogy ListView refresh the data of the open source framework, such as: V4 Pack from the Swiperefreshlayout, and set ListView, the GridView even webview in one pulltorefresh and so on. The two open source frameworks mentioned above are also used frequently. Interested readers can search their own, of course, have time to come back to all the use of the way to do a summary and comparison. Today's introduction of this framework, specifically for ListView to do Drop-down refresh and pull loading, if the simple ListView is easier and easier to understand.
1, the first introduction of Xlistview_lib library to their own demo
2, the use of steps
It uses the same procedure as the ordinary ListView, which is why it is chosen. Very simple and convenient to use.
The layout is as follows:

<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"/> 
 
</RelativeLayout> 

Use Xlistview in the ListView Way:

Package Com.itydl.xlistviewdemo; 
Import java.util.ArrayList; 
 
Import java.util.List; 
Import Me.maxwin.view.XListView; 
Import Android.os.Bundle; 
Import android.app.Activity; 
Import Android.graphics.Color; 
Import Android.view.Menu; 
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; 
    @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); /** Analog network data/private void Initdatas () {for (int i = 0; i < i++) {Datas.add ("I am the number of + i +" bars 
    According to "); }
  Private class Myadapter extends baseadapter{@Override public int GetCount () {if (Datas!= 
      ll) {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; 
 } 
     
  } 
}

Run Result:


The top just did the show and didn't have any effect on loading and refreshing. The next step is to load more and refresh in full code form.

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 last refreshed private Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) {if (Isloadmore) {//message is pull load more I 
        Nitdatas (); Isloadmore = FALse 
      Refresh finished, turn off the pull load effect 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 () { Drop-down refresh, actually send handler//Send an empty message, delay two seconds to notify 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 (n 
      EW 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 () {//Actual development should be child thread execution for (int i = 0; i < i++) {Datas.ad 
    D ("I am the first" + i + "article 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; 
 } 
  } 
}

In the top code, the refresh and load are added, and the system time value is added at the same time as the Drop-down refresh.
Note: settings can be drop-down refresh defaults to True, load more defaults to False. You need to add the following two lines of code:

The settings can be drop-down refreshed, and the default is True 
mxlistview.setpullrefreshenable (true); 
 
The settings can be pulled up to load, the default is False 
mxlistview.setpullloadenable (true); 

When the refresh is complete, be sure to close it with the following two lines of code:

Refresh finished, turn off the pull load effect
mxlistview.stoploadmore ();


Refresh finished, turn off the Drop-down refresh effect 
Mxlistview.stoprefresh (); 

Run a look at the effect:

If you want to change its style, it is also very simple. Because of its head layout and for the layout is independent open, know directly to the layout of the file to modify the display style.

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.

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.