xlistview 下拉刷新 上拉加载

来源:互联网 发布:小学网络课程 编辑:程序博客网 时间:2024/06/01 09:49

与listview加载数据不同的是集合需要new出来  不是通过解析得到的   集合.addAll()

1.  下载gitHub上XlistView上的压缩包
  .   复制view包下的三个类,,,,,注意导包的问题
    .赋值layout下面的footer布局和header的布局
    .赋值string下面的字段值   

2. oncreate方法中

        xListView.setPullRefreshEnable(true);//支持下拉刷新
        xListView.setPullLoadEnable(true);//支持上拉加载更多
        xListView.setXListViewListener(this);//设置xlistView的监听事件

        getDataFromNet();

3.设置首次加载的数据

 private void getDataFromNet() {
        AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... voids) {

                String path = "http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1";
                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 null;
            }

            @Override
            protected void onPostExecute(String s) {
                Gson gson = new Gson();

                DataDataBean dataDataBean = gson.fromJson(s, DataDataBean.class);

                //将解析到的集合数据添加到上面的大集合中
                list.addAll(dataDataBean.getData());

                //设置适配器...
                setAdapter();

                //上拉加载完成....停止加载
                xListView.stopLoadMore();
            }
        };

        asyncTask.execute();
    }

 

4.设置适配器

private void setAdapter() {
        if (myAdapter == null){

            myAdapter = new MyAdapter(MainActivity.this, list);
            xListView.setAdapter(myAdapter);
        }else {
            myAdapter.notifyDataSetChanged();
        }
    }

  5.监听事件中   如果题目要求上拉刷新新数据   下拉加载

  那么加载方法中   同第一次加载数据

  刷新方法中 :

        page_num ++;

        AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... voids) {

                String path = "http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page="+page_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 null;
            }

            @Override
            protected void onPostExecute(String s) {
                Gson gson = new Gson();

                DataDataBean dataDataBean = gson.fromJson(s, DataDataBean.class);

                //下拉刷新的数据需要添加在大集合的最前边
                list.addAll(0,dataDataBean.getData());

                //设置适配器...
                setAdapter();

                //...............设置完数据之后刷新需要停止
                xListView.stopRefresh();//停止刷新

                //System.currentTimeMillis()....当前时间的long类型的值
                Date date = new Date(System.currentTimeMillis());
                //格式化....yyyy-MM-dd HH:mm
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");

                //设置本次刷新的时间
                xListView.setRefreshTime(simpleDateFormat.format(date));
            }
        };

        asyncTask.execute();
原创粉丝点击