Pull-up load, pull-down refresh function implementation

Source: Internet
Author: User

Package com.example.my_xlistview_text;

Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.nio.channels.GatheringByteChannel;
Import java.util.ArrayList;
Import java.util.List;

Import Org.apache.http.HttpResponse;
Import org.apache.http.client.ClientProtocolException;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpGet;
Import org.apache.http.impl.client.DefaultHttpClient;

Import Com.example.adapter.MyAdapter;
Import Com.example.bean.BeanVO;
Import Com.example.fresh.XListView;
Import Com.example.fresh.XListView.IXListViewListener;
Import Com.google.gson.Gson;

Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import android.app.Activity;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.ArrayAdapter;
Import Android.widget.Toast;

public class Mainactivity extends Activity implements ixlistviewlistener{
Private Xlistview XLV;
private int flag=10;
Private list<string> list,initlist;
Private Myadapter adapter;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Find controls

XLV = (Xlistview) Findviewbyid (R.ID.XLV);
Setting the pull-up drop-down
Xlv.setpullrefreshenable (TRUE);
Xlv.setpullloadenable (TRUE);
Xlv.setxlistviewlistener (this);
List=new arraylist<string> ();
Initlist=new arraylist<string> ();
Adapter=new Myadapter (list, this);
Xlv.setadapter (adapter);
Request and Parse data
New Thread () {
public void Run () {
GetData ();
};
}.start ();

}
/**
* Request and Parse data
*/
public void GetData () {
HttpClient Request Data
HttpClient client=new defaulthttpclient ();
HttpGet get=new httpget ("http://op.juhe.cn/onebox/news/words?key=aadc8e1a325cd0b899e91639018d1769");
try {
HttpResponse response = Client.execute (get);
if (Response.getstatusline (). Getstatuscode () ==200) {
Get the data
LOG.I ("Request Data", "refresh");
InputStream is=response.getentity (). getcontent ();
BufferedReader reader=new BufferedReader (New InputStreamReader (IS));
StringBuffer sb=new StringBuffer ();
String str;
while ((Str=reader.readline ())!=null) {
Sb.append (str);
}
String jsonstr=sb.tostring ();
Gson parsing
Gson gson=new Gson ();
Beanvo Bv=gson.fromjson (Jsonstr, Beanvo.class);
List = Bv.getresult ();
InitData ();
Send Message
Message msg=handler.obtainmessage (1, initlist);
Handler.sendmessage (msg);

}
} catch (Clientprotocolexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
/**
* Number of items displayed
*/
public void InitData () {
Initlist.clear ();
if (List.size () >flag) {
for (int i = 0; i < flag; i++) {
Initlist.add (List.get (i));
}
} else {
Initlist.addall (list);
}
}
/**
* Processing messages
*/
Handler handler=new Handler () {
public void Handlemessage (Android.os.Message msg) {
if (msg.what==1) {
Get the data coming in.
List<string> lists= (list<string>) msg.obj;
Refresh Adapter Presentation Data
Adapter.setnewdata (lists);
}
};
};

/**
* Drop-down Refresh
*/
@Override
public void Onrefresh () {
TODO auto-generated Method Stub
Request data
flag=10;
New Thread () {
public void Run () {

GetData ();

};
}.start ();
OnLoad ();
}
/**
* Pull-up loading
*/
@Override
public void Onloadmore () {
TODO auto-generated Method Stub
flag+=10;
InitData ();
Adapter.setnewdata (initlist);
OnLoad ();
if (List.size () <flag) {
Toast.maketext (Mainactivity.this, "No more Data", +). Show ();
}

}
/**
* Stop loading
*/
public void OnLoad () {
Xlv.stoploadmore ();
Xlv.stoprefresh ();
}
}

Layout effect

<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"
Tools:context= ". Mainactivity ">

<com.example.fresh.xlistview
Android:id= "@+id/xlv"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
/>

</RelativeLayout>

Pull-up load, pull-down refresh function implementation

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.