zoukankan      html  css  js  c++  java
  • 通过类库library实现下拉刷新上拉加载

      首先导入类库,添加到所需的项目中

    这是布局文件

       <com.handmark.pulltorefresh.library.PullToRefreshListView
            xmlns:ptr="http://schemas.android.com/apk/res-auto"
            android:id="@+id/lv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            ptr:ptrMode="both" >
        </com.handmark.pulltorefresh.library.PullToRefreshListView>
    

     接下来写下拉刷新,上拉加载的逻辑判断

    private ArrayList<Detail> list=new ArrayList<Detail>();
        private PullToRefreshListView lv;
        private MyListAdapter adapter;
        private int p=10;
        private Handler handler = new Handler() {

            public void handleMessage(android.os.Message msg) {

                if (msg.what == 0) {
                    String data = (String) msg.obj;
                    // 解析Json数据
                    Gson gson = new Gson();
                    AllData ad = gson.fromJson(data, AllData.class);
                    list=ad.getData();
                    adapter = new MyListAdapter(MainActivity.this, list);
                    lv.setAdapter((ListAdapter) adapter);
                    adapter.notifyDataSetChanged();
                    lv.onRefreshComplete();
                }if(msg.what==1){
                    String data = (String) msg.obj;
                    // 解析Json数据
                    Gson gson = new Gson();
                    AllData ad = gson.fromJson(data, AllData.class);
                    list.addAll(ad.getData());
                    adapter.notifyDataSetChanged();
                    lv.onRefreshComplete();
                }

            };
        };

    // 找到需要显示新闻的listview
            lv = (PullToRefreshListView) findViewById(R.id.lv);
            new Thread() {
                public void run() {
                    readData();
                }

            }.start();

    //给listview设置下拉刷新和上拉加载的刷新监听事件

    lv.setOnRefreshListener(new OnRefreshListener2<ListView>() {

                @Override
                public void onPullDownToRefresh(
                        PullToRefreshBase<ListView> refreshView) {
                    // 刷新时间
                    String label = DateUtils.formatDateTime(
                            getApplicationContext(), System.currentTimeMillis(),
                            DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE
                                    | DateUtils.FORMAT_ABBREV_ALL);

                    // Update the LastUpdatedLabel
                    refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                    new Thread() {
                        public void run() {
                            try {
                                Thread.sleep(2000);
                                z=z-1;
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            readData();//下拉和初始化数据可以使用同一个方法
                            
                        }
                    }.start();

                }

                @Override
                public void onPullUpToRefresh(
                        PullToRefreshBase<ListView> refreshView) {
                    new Thread() {
                        public void run() {
                            try {
                                Thread.sleep(2000);
                                p=p+1;
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            readData1();
                            
                        }

                        
                    }.start();
                   
                }
            });

    //以下是下拉刷新和初始化数据的方法

        protected void readData() {
            try {
                URL url = new URL(
                        "http://api.expoon.com/AppNews/getNewsList/type/1/p/"+z);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setReadTimeout(8000);
                conn.setConnectTimeout(5000);
                InputStream is = conn.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                StringBuilder str = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    str.append(line);
                }
                Message msg = new Message();
                msg.what = 0;
                msg.obj = str.toString();
                handler.sendMessage(msg);
                Log.i("line", str.toString());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    //这是上拉加载的方法

        private void readData1() {
            try {
                URL url = new URL(
                        "http://api.expoon.com/AppNews/getNewsList/type/1/p/"+p);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setReadTimeout(8000);
                conn.setConnectTimeout(5000);
                InputStream is = conn.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                StringBuilder str = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    str.append(line);
                }
                Message msg = new Message();
                msg.what = 1;
                msg.obj = str.toString();
                handler.sendMessage(msg);
                Log.i("line", str.toString());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }



  • 相关阅读:
    SpringCloud 第一篇:服务的注册和发现(Eureka)
    微服务架构
    SpringMvc的工作原理
    《即时消息技术剖析与实战》学习笔记7——IM系统的消息未读
    《MySQL实战45讲》学习笔记3——InnoDB为什么采用B+树结构实现索引
    《即时消息技术剖析与实战》学习笔记6——IM系统如何保证消息的安全性
    《即时消息技术剖析与实战》学习笔记5——IM系统如何保证消息的一致性
    《即时消息技术剖析与实战》学习笔记4——IM系统如何保证消息的可靠性
    《即时消息技术剖析与实战》学习笔记3——IM系统如何保证消息的实时性
    《即时消息技术剖析与实战》学习笔记2——支持用户点对点聊天的消息收发架构
  • 原文地址:https://www.cnblogs.com/daidai123/p/5549998.html
Copyright © 2011-2022 走看看