PullPullToRefreshListView的下拉刷新,上拉加载

ublic class MainActivity extends AppCompatActivity {

    private PullToRefreshListView pull;
    private String url = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10";
    private ArrayList list_bean = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找到控件
        pull = (PullToRefreshListView) findViewById(R.id.pull);
        initView();
    }
    Handler hand = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==1)
            {
                String count = (String) msg.obj;
                try {
                    JSONObject jsonObject = new JSONObject(count);
                    JSONArray newslist = jsonObject.optJSONArray("newslist");
                    for (int i = 0; i < newslist.length(); i++) {
                        JSONObject jsonObject1 = newslist.optJSONObject(i);
                        String description = jsonObject1.optString("description");
                        String title = jsonObject1.optString("title");
                        String picUrl = jsonObject1.optString("picUrl");
                        list_bean.add(new Bean(description,title,picUrl));
                    }
                    //将下拉和上拉的条目隐藏
                    pull.onRefreshComplete();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //设置适配器就不多说了,,很容易
                pull.setAdapter(new Adapter(MainActivity.this,list_bean));
                //让他支持上拉加载
                pull.setMode(PullToRefreshBase.Mode.BOTH);
                //设置刷新的方法
                pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
                    //下拉刷新
                    @Override
                    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                        //让存储的集合清空,重新请求数据添加进集合
                        list_bean.clear();
                        //在调用一次查找数据的方法
                        initView();
                    }
                    //上拉加载
                    @Override
                    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                        //调用查找方法,自动拼接到集合中
                        initView();
                    }
                });
            }
        }
    };
    //通过线程解析地址
    public void initView()
    {
        new Thread()
        {
            @Override
            public void run() {
                try {
                    URL ur = new URL(url);
                    URLConnection urlConnection = ur.openConnection();
                    InputStream inputStream = urlConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    String read = "";
                    StringBuilder stringBuilder = new StringBuilder();
                    while((read=bufferedReader.readLine())!=null)
                    {
                        stringBuilder.append(read);
                    }
                    Message message = new Message();
                    message.obj = stringBuilder.toString();
                    message.what = 1;
                    hand.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }


}

你可能感兴趣的:(PullPullToRefreshListView的下拉刷新,上拉加载)