In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要介绍了Android怎么通过XListView实现上拉加载下拉刷新功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android怎么通过XListView实现上拉加载下拉刷新功能文章都会有所收获,下面我们一起来看看吧。
具体代码如下:
## 导入XListVIew第三方库文件。通过LinkedList将刷新数据插入到集合头部,将加载的数据放入集合尾部 ## private Context context; private View view; private String path; private XListView xlv; private LinkedList 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) msg.obj; myAdapter = new NewsFragmentAdapter(context, listData); xlv.setAdapter(myAdapter); break; case 1: LinkedList shualist = (LinkedList) 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 loadlist = (LinkedList) 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() { // 请求网络失败回调的方法 @Override public void onFailure(HttpException arg0, String arg1) { } // 请求网络成功回调的方法 @Override public void onSuccess(ResponseInfo arg0) { String info = arg0.result; Gson gson = new Gson(); LinkedList 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; }关于"Android怎么通过XListView实现上拉加载下拉刷新功能"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"Android怎么通过XListView实现上拉加载下拉刷新功能"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.