XListView实现上拉加载下拉刷新,注意事项
来源:互联网 发布:帝国文明源码下载 编辑:程序博客网 时间:2024/05/21 11:32
使用XListview注意事项:首先将下载好的开源github代码中的view包里面的三个类,xlistview,xlistviewFooter,xlistviewHeaderlayout里面的xlistview_header,xlistview_footervalues下面的strings.xml里面的代码复制进来drawable下面的一张图片还要改那些类的R文件public class Fragmentzhu extends Fragment implements XListView.IXListViewListener { @Nullable private List<DBean.DataBean> list = new ArrayList<>(); int zhi = 1; int num = 1;//页数 private MyAdapter myAdapter; private XListView xListView; public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragmentzhubeijing, container, false); xListView = view.findViewById(R.id.xlist_view); xListView.setPullLoadEnable(true); xListView.setPullRefreshEnable(true); xListView.setXListViewListener(this); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle bundle = getArguments(); zhi = bundle.getInt("key"); Log.i("zhi",zhi+""); getDataFromNet(); } private void getDataFromNet() { AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { String path = "http://api.expoon.com/AppNews/getNewsList/type/1/p"+num; try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //设置请求 connection.setRequestMethod("GET"); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); //获取 int responseCode = connection.getResponseCode(); if (responseCode == 200){ InputStream inputStream = connection.getInputStream(); String json = streamToString(inputStream,"utf-8"); return json; } } catch (Exception e) { e.printStackTrace(); } return ""; } @Override protected void onPostExecute(String json) { Log.i("--",json); Gson gson = new Gson(); DBean dBean = gson.fromJson(json, DBean.class); list.addAll(dBean.getData()); setAdapter(); xListView.stopLoadMore(); } }; asyncTask.execute(); } private void setAdapter() { if (myAdapter==null) { myAdapter = new MyAdapter(list, getActivity()); xListView.setAdapter(myAdapter); }else{ myAdapter.notifyDataSetChanged(); } } private String streamToString(InputStream inputStream, String charset) { try { InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String s = null; StringBuilder builder = new StringBuilder(); while ((s = bufferedReader.readLine()) != null){ builder.append(s); } bufferedReader.close(); return builder.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } @Override public void onRefresh() { num--; if (num>0){ refreshData(); }else{ Toast.makeText(getActivity(),"已经是第一页了",Toast.LENGTH_SHORT).show(); xListView.stopRefresh(); } } @Override public void onLoadMore() { num++; getDataFromNet(); } private void refreshData() { AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { String path = "http://api.expoon.com/AppNews/getNewsList/type/1/p"+num; try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //设置请求 connection.setRequestMethod("GET"); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); //获取 int responseCode = connection.getResponseCode(); if (responseCode == 200){ InputStream inputStream = connection.getInputStream(); String json = streamToString(inputStream,"utf-8"); return json; } } catch (Exception e) { e.printStackTrace(); } return ""; } @Override protected void onPostExecute(String json) { Log.i("--",json); Gson gson = new Gson(); DBean dBean = gson.fromJson(json, DBean.class); list.addAll(0,dBean.getData()); setAdapter(); xListView.stopRefresh(); //设置刷新时间 long currentTimeMillis = System.currentTimeMillis(); Date date = new Date(System.currentTimeMillis()); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); String time = simpleDateFormat.format(date); xListView.setRefreshTime(time); } }; asyncTask.execute(); }}
阅读全文
0 0
- XListView实现上拉加载下拉刷新,注意事项
- XListView实现上拉刷新下拉加载
- xlistview实现上拉加载,下拉刷新
- XListView实现下拉刷新和上拉加载
- 通过XListView实现上拉加载下拉刷新
- Xlistview实现上拉刷新下拉加载及item可单选
- MainActivity中Xlistview实现下拉刷新和上拉加载
- XlistView上拉刷新下拉加载
- xListView上拉刷新下拉加载
- xListView上拉加载下拉刷新
- XListView上拉加载,下拉刷新
- xlistview上拉加载下拉刷新
- XListView上拉加载下拉刷新
- XListView上拉刷新,下拉加载
- 上拉刷新--下拉加载XListView
- XListView上拉加载下拉刷新
- xlistview上拉刷新、下拉加载
- XListView上拉加载下拉刷新
- linux常用命令
- bat入门
- feurio中文版下载地址附使用教程
- windows 环境下在anaconda 3中安装python2和python3两个环境(python2和python3共存)
- 机器学习实战笔记-决策树
- XListView实现上拉加载下拉刷新,注意事项
- java.util.concurrent.locks
- rs.next()
- Android ffmpeg 3.4 JNI移植 NDK编译播放 ffserver 推送的 Rtsp 视频流
- 【学习贴】Ps终极动画练习
- 五子棋人机
- Python3-搭建Django框架
- windows 2003系统下邮件系统搭建
- Flume安装-配置-调试