pull刷新

package com.example.mylist; import java.util.ArrayList; import java.util.List; import com.example.adapter.MyAdapter; import com.example.adapter.MyAdapter1; import com.example.myutils.MyUtils; import com.example.vo.Data; import com.example.vo.News; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener; import com.handmark.pulltorefresh.library.PullToRefreshListView; import android.os.Bundle; import android.os.Handler; import android.text.format.DateUtils; import android.widget.ListView; import android.app.Activity; public class MainActivity extends Activity implements OnRefreshListener, OnLastItemVisibleListener { private PullToRefreshListView lv; int p=1; private MyAdapter1 adapter; List list_push=new ArrayList(); Handler handler=new Handler(){ public void handleMessage(android.os.Message msg) { if(msg.what==1){ List data = (List) msg.obj; System.out.println(data+"============="); list_push=data; adapter = new MyAdapter1(MainActivity.this, list_push); lv.setAdapter(adapter); } if(msg.what==2){ List data2 = (List) msg.obj; list_push.addAll(data2); adapter.notifyDataSetChanged(); } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); getdata(); } private void init() { lv = (PullToRefreshListView) findViewById(R.id.lv); lv.setOnRefreshListener(this); lv.setOnLastItemVisibleListener(this); } private void getdata() { // TODO Auto-generated method stub new Thread(){ public void run() { String str=MyUtils.Check_j(p); Gson g=new Gson(); News news=g.fromJson(str, News.class); List list=news.getData(); handler.sendMessage(handler.obtainMessage(1, list)); }; }.start(); } @Override public void onRefresh(PullToRefreshBase refreshView) { String twoInfo = DateUtils.formatDateTime(getApplicationContext(),System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); getdata(); adapter.notifyDataSetChanged(); } @Override public void onLastItemVisible() { new Thread(){ public void run() { String str=MyUtils.Check_j(p++); Gson g=new Gson(); News news=g.fromJson(str, News.class); List list=news.getData(); handler.sendMessage(handler.obtainMessage(2, list)); }; }.start(); } }

原文地址:https://www.cnblogs.com/1995yu/p/5424257.html