【Android 开源项目】下拉刷新Android-PullToRefresh介绍

目前市面上的大多数APP都包含下拉刷新的功能,今天介绍一个github上非常有名的下拉刷新开源项目,可以在项目中直接使用,非常简单。github地址:Android-PullToRefresh。

该项目不仅支持下拉刷新而且支持上拉加载,支持一下Android组件:

ListView
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager

注:该项目需要jdk 1.6或者以上才能使用

一。使用方式

1.xml布局文件添加刷新的控件

<!-- 使用PullToRefreshListView代替普通的Android组件 -->
<com.handmark.pulltorefresh.library.PullToRefreshListView  android:id="@+id/pull_to_refresh_listview" android:layout_height="fill_parent" android:layout_width="fill_parent" />

2.监听下拉刷新状态

// 设置一个监听当用户下拉的时候触发下拉刷新事件
PullToRefreshListView pullToRefreshView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);
pullToRefreshView.setOnRefreshListener(new OnRefreshListener<ListView>() {
    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        // 下拉后执行的操作
        new GetDataTask().execute();
    }
});

private class GetDataTask extends AsyncTask<Void, Void, String[]> {
    ...
    @Override
    protected void onPostExecute(String[] result) {
        // 刷新完成,调用onRefreshComplete方法
        pullToRefreshView.onRefreshComplete();
        super.onPostExecute(result);
    }
}

小例子

【Android 开源项目】下拉刷新Android-PullToRefresh介绍_第1张图片

1 . 新建项目首先引入下拉刷新lib项目, github项目主页右侧直接下载:

【Android 开源项目】下拉刷新Android-PullToRefresh介绍_第2张图片

下载后导入即可:

【Android 开源项目】下拉刷新Android-PullToRefresh介绍_第3张图片

2.activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true" />

</RelativeLayout>

3.MainActivity.java

public class MainActivity extends Activity {

    private PullToRefreshListView pullToRefreshListView;

    private LinkedList<String> mListItems;

    private ArrayAdapter<String> mAdapter;

    private String[] mStrings = { "苹果", "香蕉", "桔子", "栗子", "西瓜",
        "葡萄", "桃子", "榴莲", "提子", "哈密瓜"};

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pullToRefreshListView = (PullToRefreshListView)findViewById(R.id.pull_refresh_list);

        pullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>()
        {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> pullToRefreshBase)
            {
                String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                //更新上次更新时间
                pullToRefreshBase.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                //刷新之后执行的操作
                new GetDataTask().execute();
            }
        });

        pullToRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

            @Override
            public void onLastItemVisible() {
                Toast.makeText(MainActivity.this, "End of List!", Toast.LENGTH_SHORT).show();
            }
        });

        //构建模拟数据
        mListItems = new LinkedList<String>();
        mListItems.addAll(Arrays.asList(mStrings));
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);

        //添加适配器
        pullToRefreshListView.getRefreshableView().setAdapter(mAdapter);
    }

    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            //模拟后台操作,实际工作中可执行获取数据的请求操作
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
            }
            return mStrings;
        }

        @Override
        protected void onPostExecute(String[] result) {
            mListItems.addFirst("这是一种新的水果");
            mAdapter.notifyDataSetChanged();

            //刷新完成的时候调用onRefreshComplete
            pullToRefreshListView.onRefreshComplete();

            super.onPostExecute(result);
        }
    }
}

注:其他一些下拉刷新项目

1.android-pulltorefresh

2.PullToRefresh-ListView

3.android-pulltorefresh-and-loadmore

4.PullToRefresh

5.PullToRefreshAndLoad

6.AnimatePullToRefreshListView

7.pulltorefresh

你可能感兴趣的:(github,android,开源项目,GridView)