Drop-down refresh, pull-up loading implementation Introduction

Source: Internet
Author: User


1. Technical points
listview.setselection (int postion); Navigates to a specific entry in the ListView.
Listview.addheaderview (view view), adding a view to the ListView header
Listview.addfooterview (view); Add view to the bottom of the ListView

Listview.setonscrolllistener (this); Add a scrolling event to the ListView and listen to the scroll position again.
If you scroll to the top, the head view is displayed to refresh the data.
If scrolling to the bottom view is displayed, data loading is implemented.


2. Example
Package com.example.test;

Import java.util.ArrayList;
Import Java.util.HashMap;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.AbsListView;
Import Android.widget.AbsListView.OnScrollListener;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;
Import Android.widget.TextView;
Import Android.widget.Toast;

public class MainActivity1 extends Activity implements Onscrolllistener {

private static final String TAG = "mainactivity";

Private ListView ListView;
Private View Moreview,moreviewheader; Load More pages

Private Simpleadapter adapter;
Private arraylist
private int lastitem;
private int count;
private int firstvisibleitem;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main1);

ListView = (ListView) Findviewbyid (R.id.listview);
Moreview = Getlayoutinflater (). Inflate (r.layout.load1, NULL);
Moreviewheader = Getlayoutinflater (). Inflate (r.layout.load, NULL);
ListData = new arraylist
Listview.setdivider (NULL);

Preparedata (); Preparing data
Count = Listdata.size ();

adapter = new Simpleadapter (this, ListData, R.layout.item,
New string[] {"Itemtext"}, new int[] {r.id.itemtext});

Listview.addheaderview (Moreviewheader);
Listview.addfooterview (Moreview); Add bottom view, be sure to add before setadapter, otherwise you will get an error.

Listview.setadapter (adapter); Set Adapter

Listview.setselection (1);
Listview.setonscrolllistener (this); Set scrolling events for a ListView
}

private void Preparedata () {//Prepare data
for (int i = 0; i < i++) {
hashmap<string, string> map = new hashmap<string, string> ();
Map.put ("Itemtext", "test data" + i);
Listdata.add (map);
}

}

private void Loadmoredata () {//Load more data
Count = Adapter.getcount ();
for (int i = count; I < count + 5; i++) {
hashmap<string, string> map = new hashmap<string, string> ();
Map.put ("Itemtext", "test data more---" + i);
Listdata.add (map);
}
Count = Listdata.size ();
}

@Override
public void Onscroll (Abslistview view, int firstvisibleitem,
int visibleitemcount, int totalitemcount) {

LOG.I (TAG, "firstvisibleitem=" + Firstvisibleitem
+ ",, visibleitemcount=" + VisibleItemCount + ",,, Totalitemcount"
+ Totalitemcount);
This.firstvisibleitem=firstvisibleitem;
LastItem = This.firstvisibleitem + visibleItemCount-2; Minus 1 because it adds a addfooterview.

}

@Override
public void onscrollstatechanged (Abslistview view, int scrollstate) {
LOG.I (TAG, "scrollstate=" + scrollstate);
Update when drop-to-idle is, and the last item number equals the total number of data

System.out.println (lastitem + "= = =" + count);
if (LastItem = = Count && Scrollstate = = this. Scroll_state_idle) {
LOG.I (TAG, "pull to the bottom");
Moreview.setvisibility (view.visible);
Mhandler.sendemptymessage (0);
}


if (this.firstvisibleitem==0) {
((TextView) Moreviewheader.findviewbyid (R.id.button1)). SetText ("refreshing ...");
Moreviewheader.setvisibility (view.visible);
Mhandler.sendemptymessage (1);
}
}

Statement Handler
Private Handler Mhandler = new Handler () {
public void Handlemessage (Android.os.Message msg) {
Switch (msg.what) {
Case 0:
try {
Thread.Sleep (3000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
Loadmoredata (); Load more data, where you can use asynchronous loading
Adapter.notifydatasetchanged ();
Moreview.setvisibility (View.gone);

LOG.I (TAG, "load more Data");
Listview.setselection (LastItem);
Break
Case 1:
try {
Thread.Sleep (3000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}

Adapter.notifydatasetchanged ();
Moreviewheader.setvisibility (View.gone);
Listview.setselection (1);
Break
Default
Break
}
};
};
}

2, use the Open source framework Pulltorefresh, to GitHub search.

Drop-down refresh, pull-up loading implementation Introduction

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.