Practical drop-down refresh pull-up load data function

Source: Internet
Author: User

Everyone must have been aware of Taobao Ah Tmall some shopping website merchandise drop-down refresh info pull-up load item details

See Effect


Here we use the Xlistview package to copy some configuration code to implement these functions

First of all prepare to have Xlistview package we create a project such as demos and then put the Java file under the Me.maxwei.view package R path to your package path.

after the ListView of Main in XML is OK

Next, we'll look at the code for the main mainactivity.

Note that the two methods of implementing implements Ixlistviewlistener look at the code

Package Com.example.demos;

Import java.util.ArrayList;
Import java.util.List;



Import Me.maxwin.view.XListView;
Import Me.maxwin.view.XListView.IXListViewListener;
Import Android.os.Bundle;
Import Android.os.Handler;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.widget.ArrayAdapter;
Import Android.widget.Toast;

public class Mainactivity extends Activity implements Ixlistviewlistener {

Private Xlistview LV;
Private arrayadapter<string> adapter;
Private Handler hand;
Private list<string> li= new arraylist<string> ();
private int num = 18;//Set initial data with 18 bars

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Loading data
GetData ();
Listview
LV = (Xlistview) Findviewbyid (r.id.lv);
Configuration information
Lv.setpullloadenable (TRUE);//support pull-down to load data
Lv.setpullrefreshenable (TRUE);//support pull up Refresh data
Lv.setxlistviewlistener (this);//support triggering of various events
Set Adapter
adapter = new Arrayadapter<string> (this, R.layout.list_item, Li);
Lv.setadapter (adapter);
Used to use thread time
Hand = new Handler ();
}

/**
* Initialization data and subsequent pull-down loading data into the collection
*/
private void GetData () {
TODO auto-generated Method Stub
for (int i = 0; i < num; i++) {
Li.add ("Hellow fanaw!" + i);
}
}

Refresh
@Override
public void Onrefresh () {
Start thread
Hand.postdelayed (New Runnable () {

@Override
public void Run () {
Can do refresh processing

}

}, 2000);
Lv.stoprefresh ();//Stop Refresh
Toast.maketext (This, "refresh completed", 0). Show ();
}

Load
@Override
public void Onloadmore () {
Start thread
Hand.postdelayed (New Runnable () {

@Override
public void Run () {
Do some loading operations
num + = 10;
GetData ();
Adapter.notifydatasetchanged ();
}

}, 2000);

Lv.stoploadmore ();//Stop loading

Toast.maketext (This, "li.lenth==" + li.size (), 0). Show ();
}

}





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.