使用XListview注意事项: 首先将下载好的开源github代码中的 view包里面的三个类,xlistview,xlistviewFooter,xlistviewHeader layout里面的xlistview_header,xlistview_footer values下面的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(); } }
XListView实现上拉加载下拉刷新,注意事项
最新推荐文章于 2017-11-08 13:45:28 发布