PullToRefreshListView不请求网络

来源:互联网 发布:linux vi强制退出命令 编辑:程序博客网 时间:2024/06/05 09:07

//主方法

package com.example.pull;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import com.handmark.pulltorefresh.library.ILoadingLayout;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshListView;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {    private PullToRefreshListView pullToRefreshListView;    private Handler myHandler=new Handler();    private List<String> lists=new ArrayList<String>();    private MyAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_lv);        initLv();        initData();        setAdapter();    }    public void initData(){        for(int i=0;i<20;i++){            lists.add("烦烦烦"+i+"条烦");        }    }    public void setAdapter(){        if(adapter==null){            adapter=new MyAdapter();            pullToRefreshListView.setAdapter(adapter);        }else{            adapter.notifyDataSetChanged();        }    }    public void addtoTop(){        List<String> listss=new ArrayList<String>();        for(int i=0;i<20;i++){            listss.add("滚蛋吧肿瘤君");        }        lists.addAll(0,listss);    }    public void addtoBottom(){        List<String> listss=new ArrayList<String>();        for(int i=0;i<20;i++){            listss.add("不容易啊");        }        lists.addAll(listss);    }    public void initLv(){        //设置刷新模式 ,both代表支持上拉和下拉,pull_from_end代表上拉,pull_from_start代表下拉        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);/*这里通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的状态的区别,第一个true 代表下来状态 ,第二个true 代表上拉的状态如果想区分上拉和下拉状态的不同,可以分别设置*/        ILoadingLayout startLabels = pullToRefreshListView.getLoadingLayoutProxy(true, false);        startLabels.setPullLabel("下拉刷新");        startLabels.setRefreshingLabel("正在拉");        startLabels.setReleaseLabel("放开刷新");        ILoadingLayout endLabels = pullToRefreshListView.getLoadingLayoutProxy(false, true);        endLabels.setPullLabel("上拉刷新");        endLabels.setRefreshingLabel("正在载入...");        endLabels.setReleaseLabel("放开刷新...");/*如果Mode设置成Mode.BOTH,需要设置刷新Listener为OnRefreshListener2,并实现onPullDownToRefresh()、onPullUpToRefresh()两个方法。  如果Mode设置成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要设置刷新Listener为OnRefreshListener,同时实现onRefresh()方法。  当然也可以设置为OnRefreshListener2,但是Mode.PULL_FROM_START的时候只调用onPullDownToRefresh()方法,Mode.PULL_FROM_END的时候只调用onPullUpToRefresh()方法.  加载数据完成后 必须 调用下 onRefreshComplete() 完成关闭 header,footer视图*/        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {            @Override            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {//下拉刷新的回调                //下拉刷新的数据,显示在listview列表的最上面                addtoTop();                myHandler.postDelayed(new Runnable() {                    @Override                    public void run() {                        //刷新完成,必须在异步下完成                        pullToRefreshListView.onRefreshComplete();                        //刷新适配器                        setAdapter();                    }                },1000);            }            @Override            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {//上拉加载的回调                //加载更多的数据,添加到集合列表的最后面                addtoBottom();                myHandler.postDelayed(new Runnable() {                    @Override                    public void run() {                        //刷新完成,必须在异步下完成                        pullToRefreshListView.onRefreshComplete();                        //刷新适配器                        setAdapter();                    }                },1000);            }        });    }    class MyAdapter extends BaseAdapter {        @Override        public int getCount() {            return lists.size();        }        @Override        public Object getItem(int position) {            return null;        }        @Override        public long getItemId(int position) {            return 0;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            TextView textView=new TextView(MainActivity.this);            textView.setText(lists.get(position));            return textView;        }    }}
//布局

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:ptr="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" tools:context="com.example.pull.MainActivity">    <com.handmark.pulltorefresh.library.PullToRefreshListView        android:layout_width="368dp"        android:layout_height="495dp"        android:id="@+id/pull_lv"        ptr:ptrDrawable="@drawable/default_ptr_flip"        ptr:ptrAnimationStyle="flip"        ptr:ptrHeaderBackground="#383838"        ptr:ptrHeaderTextColor="#FFFFFF"        tools:layout_editor_absoluteY="8dp"        tools:layout_editor_absoluteX="8dp"></com.handmark.pulltorefresh.library.PullToRefreshListView></android.support.constraint.ConstraintLayout>

原创粉丝点击