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();
            }
            
        }



  • 相关阅读:
    IIS 浏览aspx页面出现无法显示XML页的解决方法分享
    好看的导航菜单
    C# FileUpload使用过程遇到的问题
    javascript之数组操作
    浅谈.NET下的多线程
    项目分页通用页
    npoi
    react webpack.config.js的配置及注释
    JS 解析Json 方法
    json解析
  • 原文地址:https://www.cnblogs.com/daidai123/p/5549998.html
Copyright © 2011-2022 走看看