xlistview 下拉刷新 上拉加载

与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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值