码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-05-03 16:18:13      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

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();

}

}

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

标签:

原文地址:http://www.cnblogs.com/zhengyanyan/p/5455195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!