开发---【下拉加载】

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 这里定义一个集合,用来放加载的数据
        weixinList = new ArrayList<Details>();
        path = "http://v.juhe.cn/weixin/query";
        // 加载数据
        loadData();
        // 设置滑动监听
        listView.setOnScrollListener(new OnScrollListener() {
 
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
                    // 判断一下当前的最后一个条目是不是集合的长度减一
                    int lastVisiblePosition = view.getLastVisiblePosition();
                    if (lastVisiblePosition == weixinList.size() - 1) {
                        // 加载数据,就是再执行一次异步任务
                        // 将页面++
                        positon++;
                        // 再次加载数据
                        loadData();
 
                    }
                }
 
            }
 
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
 
            }
        });
    }
 
    /**
     * 加载数据,开启异步任务
     */
    private void loadData() {
        MyAsynTask asynTask = new MyAsynTask();
        asynTask.execute();
    }
 
    private MyAdapter myAdapter;
 
    class MyAsynTask extends AsyncTask<Void, Void, String> {
 
        private ProgressDialog progressDialog;
 
        @Override
        protected void onPreExecute() {
 
            super.onPreExecute();
            // 展示一个进度条
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setTitle("人家正在努力加载");
            progressDialog.show();
        }
 
        @Override
        protected String doInBackground(Void... params) {
            try {
                // 实例化客户端对象
                HttpClient httpClient = new DefaultHttpClient();
                // 定义一个post
                HttpPost httpPost = new HttpPost(path);
                // 如果你不知道如何测试post请求到底通没通
                // 你需要自己拼串测试
                List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
                parameters.add(new BasicNameValuePair("key",
                        "0917c812f187b92e025c9b7c2d4e59b5"));
                parameters
                        .add(new BasicNameValuePair("pno", (positon + 1) + ""));
 
                HttpEntity entity = new UrlEncodedFormEntity(parameters);
                httpPost.setEntity(entity);
 
                // 执行post请求
 
                HttpResponse httpResponse = httpClient.execute(httpPost);
                int statusCode = httpResponse.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    InputStream inputStream = httpResponse.getEntity()
                            .getContent();
                    String streamToStr = streamToStr(inputStream);
                    return streamToStr;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
 
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Fragment2.this.result = result;
            // 解析字符串
            Gson gson = new Gson();
            // json
            Weixin fromJson = gson.fromJson(result, Weixin.class);
 
            // 每次数据加载上来,都获取要展示的那个集合
            ArrayList<Details> list = fromJson.result.list;
            // 添加所有的数据
            weixinList.addAll(list);
            // 消失进度条
            progressDialog.dismiss();
            // 第一次的时候,adapter是null,创建,设置数据适配器
            if (myAdapter == null) {
                myAdapter = new MyAdapter();
                listView.setAdapter(myAdapter);
            } else {
                // 之后每一次都刷新数据
                myAdapter.notifyDataSetChanged();
            }
        }
    }
 
posted @ 2016-04-05 11:57  艾-L  阅读(126)  评论(0编辑  收藏  举报