使用PullToRefresh实现下拉刷新和上拉加载

来源:互联网 发布:金汇宝软件 编辑:程序博客网 时间:2024/05/18 21:50

PullToRefresh是一套实现非常好的下拉刷新库,它支持:

1.ListView

2.ExpandableListView

3.GridView

4.WebView

等多种常用的需要刷新的View类型,而且使用起来也十分方便。


首先添加依赖:

compile 'com.jwenfeng.pulltorefresh:library:1.0.3'

布局:

<?xml version="1.0" encoding="utf-8"?><com.jwenfeng.library.pulltorefresh.PullToRefreshLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:id="@+id/mpull"    android:layout_height="match_parent">    <ListView        android:id="@+id/mlv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"></ListView></com.jwenfeng.library.pulltorefresh.PullToRefreshLayout>

Main

package com.bawei.com.myapplication;import android.os.Build;import android.os.Bundle;import android.os.Handler;import android.support.annotation.RequiresApi;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.widget.ArrayAdapter;import android.widget.ListView;import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;import java.util.LinkedList;public class MainActivity extends AppCompatActivity {    private ListView mlistview;    //开发者可以使用 PullToRefresh 对各种控件实现下拉刷新或者上拉加载以及可以自定义刷新和加载部分的视图。    private PullToRefreshLayout mpullto;    private LinkedList<String> mListItems;    private ArrayAdapter<String> mAdapter;    private int mItemCount = 20;    public static final  String TAG="MainActivity";    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mpullto= (PullToRefreshLayout) findViewById(R.id.mpull);        mlistview= (ListView) findViewById(R.id.mlv);        //初始化数据        initDatas();        mAdapter = new ArrayAdapter<String>(this,                android.R.layout.simple_list_item_1, mListItems);        mlistview.setAdapter(mAdapter);        mpullto.setLayoutMode(MODE_APPEND);//设置模板        mpullto.setRefreshListener(new BaseRefreshListener() {            @Override            public void refresh() {                Log.i(TAG, "refresh: ");                new Handler().postDelayed(new Runnable() {                    @Override                    public void run() {                        mpullto.finishRefresh();                    }                }, 3000);            }            @Override            public void loadMore() {                Log.i(TAG, "loadMore: ");                new Handler().postDelayed(new Runnable() {                                              @Override                                              public void run() {                                                  mpullto.finishLoadMore();                                              }                                          }, 3000                );            }        });    }    private void initDatas()    {        // 初始化数据和数据源        mListItems = new LinkedList<String>();        for (int i = 0; i < mItemCount; i++)        {            mListItems.add("" + i);        }    }}

效果图: