PullToRefresh加载及刷新

来源:互联网 发布:js plumb 编辑:程序博客网 时间:2024/06/06 08:47

首先要导入PullToRefreshLibrary或者依赖compile ‘com.github.userswlwork:pull-to-refresh:1.0.0’;但我个人比较习惯用依赖,因为在我们导入library包的时候,而我们而我们要找到的pulltorefreshlistview控件则选在通过报名.类名的方式获取,而且里面的属性没有快捷键,而依赖则有,这样可以有更高的效率;

布局:

<?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:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.bwie.pulltorefresh.MainActivity"><com.handmark.pulltorefresh.library.PullToRefreshListView    android:layout_width="match_parent"    android:id="@+id/lv"    android:layout_height="match_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView></android.support.constraint.ConstraintLayout>
 
Main:
import android.os.AsyncTask;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.ArrayAdapter;import android.widget.ListView;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshListView;import java.util.Arrays;import java.util.LinkedList;/** * 接入PullToRefresh * 1.引入Library * 2.依赖model * 3. 布局文件中使用可刷新的控件 * 4. 设置可刷新监听-->  mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() *      设置可以上拉  mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH); */public class MainActivity extends AppCompatActivity {    private LinkedList<String> mListItems;    private PullToRefreshListView mPullRefreshListView;    private ArrayAdapter<String> mAdapter;    private String[] mStrings = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",            "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",            "Allgauer Emmentaler"};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mPullRefreshListView = (PullToRefreshListView) findViewById(                R.id.pull_refresh_list);        //默认可以下拉, 设置BOTH后也可以上啦;        mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);        mListItems = new LinkedList<>();        mListItems.addAll(Arrays.asList(mStrings));            mAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, mListItems);        mPullRefreshListView.setAdapter(mAdapter);            //设置监听,PullToRefreshBase.OnRefreshListener2,        mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {                @Override            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {                new MAsyncTask().execute();            }            @Override            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {                new MAsyncTask2().execute();            }        });    }    class MAsyncTask extends AsyncTask<Void,Void,String[]>{            @Override            protected String[] doInBackground(Void... voids) {                try {                    Thread.sleep(2000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            return new String[0];        }        @Override        protected void onPostExecute(String[] strings) {            super.onPostExecute(strings);            mListItems.addFirst("refresh item haha ...");            mAdapter.notifyDataSetChanged();            //让刷新UI隐藏;            mPullRefreshListView.onRefreshComplete();        }    }    /**     * 处理上拉加载的逻辑     */    class MAsyncTask2 extends AsyncTask<Void,Void,String[]>{        @Override        protected String[] doInBackground(Void... voids) {            try {                Thread.sleep(2000);            } catch (InterruptedException e) {                e.printStackTrace();            }            return new String[0];        }        @Override        protected void onPostExecute(String[] strings) {            super.onPostExecute(strings);            // 原先集合调用addAll(新集合)            mListItems.addLast("loadMore Item haha1 ....");            mListItems.addLast("loadMore Item haha2 ....");            mListItems.addLast("loadMore Item haha3 ....");            mAdapter.notifyDataSetChanged();            //让刷新UI隐藏;            mPullRefreshListView.onRefreshComplete();        }    }}

原创粉丝点击