标签:
如今上拉刷新,下拉加载更多已经是浩如烟海的app的标配了
最近正好有相关的需要就去学习了一下,还是那句老话凡事都靠自己来
搞实在不是一件好事,费时费力不说可能还是在做无用功,不过自己研究学习
还是很有必要的。不多说我用到的第三方框架叫xlistview这是它的托管地址:
https://github.com/Maxwin-z/XListView-Android
下载下来以后
把解压出来的内容拷贝到自己的项目下(可以自己建一个包专门存放,我是这样做的)
要注意的是layout,String和drawable下也有几个对应的文件,这些也要考进去。
然后为listview写布局
注意:<com.xxxxxxx你自己的对应文件的位置xxx>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#f0f0f0"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <com.example.guwie.yaoren.ui.view.XListView android:id="@+id/xListView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000"> </com.example.guwie.yaoren.ui.view.XListView> </LinearLayout>
为布局文件写对应的java文件
public class TaskList extends AppCompatActivity implements XListView.IXListViewListener { //创建xlistview对象 private XListView mListView; //创建数据 List<ItemBean> dataList = new ArrayList<>(); private Object item; int page = 1; int maxpage = 0; //用来执行耗时操作 private Handler mHandler; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tasklist); //指定布局中的xlistview控件 mListView = (XListView) findViewById(R.id.xListView); mListView.setPullLoadEnable(true); mListView.setPullRefreshEnable(true); getTaskList(page); mListView.setAdapter(new MyAdapter(TaskList.this, dataList)); mListView.setXListViewListener(this); mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ case 0x001: Toast.makeText(TaskList.this , "已无更多数据" , Toast.LENGTH_SHORT).show(); break; case 0x002: Toast.makeText(TaskList.this , "未找到数据" , Toast.LENGTH_SHORT).show(); break; } } }; } //获得数据后要执行onLoad()方法,否则刷新会一直进行停不下来 private void onLoad() { mListView.stopRefresh(); mListView.stopLoadMore(); mListView.setRefreshTime("刚刚"); } @Override public void onRefresh() { dataList.clear(); page = 1; mHandler.post(new Runnable() {//执行耗时操作 @Override public void run() { getTaskList(page); mListView.setAdapter(new MyAdapter(TaskList.this, dataList)); } }); onLoad(); Log.i("------------------", "onRefresh"); } @Override public void onLoadMore() { mHandler.post(new Runnable() { @Override public void run() { if(page < maxpage){ getTaskList(++page); mListView.deferNotifyDataSetChanged(); }else{ Message msg = new Message(); msg.what=0x001; mHandler.sendMessage(msg); } // getTaskList(++page); // mListView.deferNotifyDataSetChanged(); //mListView.setAdapter(new MyAdapter(TaskList.this, dataList)); } }); onLoad(); Log.i("------------------", "onLoadMore"); } /*** * 获取list数据 volley 方法 post string数据 得到 json数据 服务端由php编写 * * */ public void getTaskList(int pages) { Toast.makeText(TaskList.this , pages+"" , Toast.LENGTH_SHORT).show(); RequestQueue mreuestQueut = Volley.newRequestQueue(this); final Gson gson = new Gson(); JsonObjectRequest jsreq = new JsonObjectRequest(Request.Method.GET, TASK_LIST_DATE+"page="+pages, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { Message msg = new Message(); try { int code = jsonObject.getInt("code"); if(code == 200){ int size = jsonObject.getJSONArray("data").length(); for(int i=0; i<size ; i++){ JSONObject dataObject = jsonObject.getJSONArray("data").getJSONObject(i); String title = dataObject.getString("title"); String price = dataObject.getString("fee"); String explain = dataObject.getString("description"); dataList.add(new ItemBean(title , explain , price , "3km")); } maxpage = ++page; page = page-1; }else if(code == 205){ //已无更多数据 //maxpage = page; page = maxpage; msg.what=0x001; mHandler.sendMessage(msg); }else if(code == 404){ //未找到数据 msg.what = 0x002; mHandler.sendMessage(msg); } } catch (JSONException e) { e.printStackTrace(); Log.i("----------------", "客户端内部错误"); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.i("----------------","服务器出错"+volleyError.toString()); } }); mreuestQueut.add(jsreq); } }
学习之处参考的demo:http://www.thinksaas.cn/group/topic/348557/
感谢作者
标签:
原文地址:http://www.cnblogs.com/wobeinianqing/p/5064740.html