Android-PullToRefresh

来源:互联网 发布:ni卸载软件 编辑:程序博客网 时间:2024/04/27 17:54

Android-PullToRefresh

Library下载地址:

https://github.com/chrisbanes/Android-PullToRefresh

1、导入Library,设置Listview layout文件中定义PullToRefreshListView Xml代码 :

<!--     The PullToRefreshListView replaces a standard ListView widget. -->    <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" />

2、找控件

1.  PullToRefreshListView mListView = (PullToRefreshListView) findViewById(R.id.list_view);//如果xml属性设置过在这里就不需要设置属性  2.  mListView.setMode(Mode.BOTH);  //设置属性的几种参数类型•   Mode.BOTH:同时支持上拉下拉 •   Mode.PULL_FROM_START:只支持下拉Pulling Down •   Mode.PULL_FROM_END:只支持上拉Pulling Up 

3、实现监听方法。

如果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的时候只调用 onPullUpToRefresh()方法.

4、举例代码实现:

package com.example.main;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.widget.ArrayAdapter;import android.widget.ListView;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;import com.handmark.pulltorefresh.library.PullToRefreshListView;public class MainActivity extends Activity {    private PullToRefreshListView pull_refresh_list;    private int TAG = 1;    private ArrayAdapter<String> adapter;    // 总集合    private ArrayList<String> mList_all = new ArrayList<String>();    private Handler handler = new Handler() {        public void handleMessage(android.os.Message msg) {            if (TAG == 1) {            if(adapetr=null){                adapter = new ArrayAdapter<String>(MainActivity.this,                        android.R.layout.simple_list_item_1,                        android.R.id.text1, mList_all);                // 设置适配器                pull_refresh_list.setAdapter(adapter);                // 刷新适配器之后停止动画                pull_refresh_list.onRefreshComplete();            } else {                adapter.notifyDataSetChanged();                // 刷新适配器之后停止动画                pull_refresh_list.onRefreshComplete();            }        }           };    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 找控件        pull_refresh_list = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);        pull_refresh_list.setMode(Mode.BOTH);        // 设置初期数据        initData();        pull_refresh_list                .setOnRefreshListener(new OnRefreshListener2<ListView>() {                    // 下拉Pulling Down                    @Override                    public void onPullDownToRefresh(                            PullToRefreshBase<ListView> refreshView) {                        // 下拉的时候数据重置                        mList_all.clear();                        initData();                    }                    // 上拉Pulling Up                    @Override                    public void onPullUpToRefresh(                            PullToRefreshBase<ListView> refreshView) {                        // 上拉的时候添加选项                        initData();                    }                });    }    private void initData() {        ArrayList<String> mListItems = new ArrayList<String>();        for (int i = 1; i <= 15; i++) {            mListItems.add("Item " + Integer.toString(i));        }        // 添加到总集合        mList_all.addAll(mListItems);        handler.obtainMessage(TAG).sendToTarget();    }}

5、运行结果

这里写图片描述
这里写图片描述
这里写图片描述

1 0