XListView实现上拉加载下拉刷新 Android通过XListView实现上拉加载下拉刷新功能
辉辉73 人气:0想了解Android通过XListView实现上拉加载下拉刷新功能的相关内容吗,辉辉73在本文为您仔细讲解XListView实现上拉加载下拉刷新的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:XListView,上拉加载,下拉刷新,下面大家一起来学习吧。
## 导入XListVIew第三方库文件。通过LinkedList将刷新数据插入到集合头部,将加载的数据放入集合尾部 ## private Context context; private View view; private String path; private XListView xlv; private LinkedList<Data> listData; private Handler handler = new Handler() { @SuppressWarnings("unchecked") public void handleMessage(android.os.Message msg) { int code = msg.arg1; if(msg.arg1==400){ Toast.makeText(context, "数据格式解析不正确", 0).show(); } else{ NewsFragmentAdapter myAdapter = null; switch (code) { case 0: // 默认 listData = (LinkedList<Data>) msg.obj; myAdapter = new NewsFragmentAdapter(context, listData); xlv.setAdapter(myAdapter); break; case 1: LinkedList<Data> shualist = (LinkedList<Data>) msg.obj; myAdapter = new NewsFragmentAdapter(context, shualist); for (int i = 0; i < shualist.size(); i++) { listData.addFirst(shualist.get(i)); } myAdapter.notifyDataSetChanged(); stopLoadOrRefresh(); break; case 2: LinkedList<Data> loadlist = (LinkedList<Data>) msg.obj; myAdapter = new NewsFragmentAdapter(context, loadlist); for (int i = 0; i < loadlist.size(); i++) { listData.addLast(loadlist.get(i)); } myAdapter.notifyDataSetChanged(); stopLoadOrRefresh(); break; default: break; } } }; }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context=getActivity(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment, container, false); path = getArguments().getString("path"); // 配置Xlist xlv = (XListView) view.findViewById(R.id.xlv); // 获得控件 xlv.setPullRefreshEnable(true); // 设置可以下拉刷新 xlv.setPullLoadEnable(true); // 设置可以上拉加载 xListViewListener(); // 调用XListView的监听方法 if(path != null){ getMessage(path, 0); // 调用方法请求数据 } itemClickListner(); // 调用XListView条目点击监听方法 return view; } private void itemClickListner() { xlv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Intent intent=new Intent(context, WebViewActivity.class); intent.putExtra("path", listData.get(arg2-1).article_url); startActivity(intent); } }); } public void xListViewListener() { xlv.setXListViewListener(new IXListViewListener() { // 当下拉刷新时实现的操作 @Override public void onRefresh() { getMessage(path, 1); // 请求网络,将新请求的内容加载到集合前端 } // 当上拉加载时实现的操作内容 @Override public void onLoadMore() { // 请求网络,将请求的新内容加载到集合后端 getMessage(path, 2); } }); } public void getMessage(String path, final int code) { HttpUtils utils = new HttpUtils(); // 设置刷新间隔时间,如果不设置默认一分钟后才能刷新出新内容 utils.configCurrentHttpCacheExpiry(0); utils.send(HttpMethod.GET, path, new RequestCallBack<String>() { // 请求网络失败回调的方法 @Override public void onFailure(HttpException arg0, String arg1) { } // 请求网络成功回调的方法 @Override public void onSuccess(ResponseInfo<String> arg0) { String info = arg0.result; Gson gson = new Gson(); LinkedList<Data> list = gson.fromJson(info, ModelBean.class).data; Message message = new Message(); if(list != null){ message.arg1 = code; message.obj = list; }else{ message.arg1=400; } handler.sendMessage(message); } }); } private void stopLoadOrRefresh() { // 停止加载 xlv.stopLoadMore(); // 停止刷新 xlv.stopRefresh(); // 最近一次刷新时间 xlv.setRefreshTime(getTime()); } public String getTime() { // 格式化时间 SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss"); Date date = new Date(); String time = format.format(date); return time; }
加载全部内容