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.监听事件的实现方法中,刷新方法中,集合添加到最前,上拉加载中,集合添加到最后

@Override
    public void onRefresh() {
        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();
    }

    /**
     * 这是上拉加载的时候调用的方法
     *
     * 上拉加载更多......例如:让他请求第一页数据
     */
    @Override
    public void onLoadMore() {

        //再次调用
        getDataFromNet();
    }


   

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐