handler结合子线程实现pulltorefresh刷新加载;

package com.baidu.yuekao;

import java.util.ArrayList;
import java.util.List;

import com.baidu.adapter.MyAdapter1;
import com.baidu.myutils.MyUtils;
import com.baidu.vo.Content;
import com.baidu.vo.MyData;
import com.baidu.vo.MyResult;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.GridView;

public class ShowActivity extends Activity{
private String id;

private PullToRefreshGridView gv;
List<MyData> list_push=new ArrayList<MyData>();
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if(msg.what==1){
List<MyData> ldata = (List<MyData>) msg.obj;
gv.setAdapter(new MyAdapter1(ShowActivity.this,ldata));
gv.onRefreshComplete();
}
if(msg.what==2){
List<MyData> ldata = (List<MyData>) msg.obj;

gv.setAdapter(new MyAdapter1(ShowActivity.this,ldata));
gv.onRefreshComplete();
}
if(msg.what==3){
List<MyData> ldata = (List<MyData>) msg.obj;
list_push.addAll(ldata);

gv.setAdapter(new MyAdapter1(ShowActivity.this,list_push));
new MyAdapter1(ShowActivity.this,list_push).notifyDataSetChanged();


}
};
};

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
init();
}

private void init() {
gv = (PullToRefreshGridView) findViewById(R.id.gv);
Intent intent=getIntent();
id = intent.getStringExtra("id");
getdata(0,1);

gv.setOnRefreshListener(new OnRefreshListener2<GridView>() {

public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
// TODO Auto-generated method stub
getdata(20,2);
}

public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
// TODO Auto-generated method stub
getdata(40,3);
}
});


}

private void getdata(final int pn,int what) {
new Thread(){
public void run() {
String str=MyUtils.Check_j(
MyUtils.curl+"&catalog_id=+"+id+"&pn="+pn+"&rn=20");
Gson gson=new Gson();
Content content=gson.fromJson(str, Content.class);
List<MyData> list=content.getResult().getData();
System.out.println(list+"=============");
handler.sendMessage(handler.obtainMessage(what, list));
};

}.start();

}

}

posted @ 2016-05-03 15:39  zyy明天你好  阅读(120)  评论(0编辑  收藏  举报