Android下拉/上拉刷新ListView之Android-PullToRefresh

Android下拉/上拉刷新ListView之Android-PullToRefresh

Android下拉和上拉刷新ListView列表內容的的一個優秀開源框架,在github上的項目鏈接地址:https://github.com/chrisbanes/Android-PullToRefresh

該PullToRefresh第三方控件功能強大,使用方便。
具體使用方法:

(1)首先到github上把該項目下載解壓,導入到Eclipse的工程中。
(2)將Android-PullToRefresh作爲一個lib引用到自己的項目中。
然後直接使用即可。
現在給出一個Demo。

import java.util.LinkedList;  
  
import com.handmark.pulltorefresh.library.PullToRefreshBase;  
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;  
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;  
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;  
import com.handmark.pulltorefresh.library.PullToRefreshListView;  
  
import android.support.v7.app.ActionBarActivity;  
import android.widget.ArrayAdapter;  
import android.widget.ListView;  
import android.widget.Toast;  
import android.os.Bundle;  
import android.os.Handler;  
  
public class MainActivity extends ActionBarActivity {  
  
    private PullToRefreshListView mPullRefreshListView;  
    private LinkedList<String> mListItems;  
    private ArrayAdapter<String> mAdapter;  
  
    // 數據  
    private int DATA = 0;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);  
  
        // Mode.BOTH:支持下拉和上拉刷新。  
        mPullRefreshListView.setMode(Mode.BOTH);  
  
        mPullRefreshListView  
                .setOnRefreshListener(new OnRefreshListener2<ListView>() {  
  
                    // 下拉  
                    @Override  
                    public void onPullDownToRefresh(  
                            PullToRefreshBase<ListView> refreshView) {  
                        Toast.makeText(getApplicationContext(), "下拉刷新",  
                                Toast.LENGTH_SHORT).show();  
  
                        addItem();  
                    }  
  
                    // 上拉  
                    @Override  
                    public void onPullUpToRefresh(  
                            PullToRefreshBase<ListView> refreshView) {  
                        Toast.makeText(getApplicationContext(), "上拉刷新",  
                                Toast.LENGTH_SHORT).show();  
  
                        addItem();  
                    }  
                });  
  
        // 列表到底,即看到最後一個元素。  
        mPullRefreshListView  
                .setOnLastItemVisibleListener(new OnLastItemVisibleListener() {  
  
                    @Override  
                    public void onLastItemVisible() {  
                        Toast.makeText(getApplication(), "已經到底!",  
                                Toast.LENGTH_SHORT).show();  
                    }  
                });  
  
        ListView actualListView = mPullRefreshListView.getRefreshableView();  
  
        mListItems = new LinkedList<String>();  
        mAdapter = new ArrayAdapter<String>(this,  
                android.R.layout.simple_list_item_1, mListItems);  
        actualListView.setAdapter(mAdapter);  
    }  
  
    // 添加數據  
    private void addItem() {  
  
        new Handler().postDelayed(new Runnable() {  
  
            @Override  
            public void run() {  
                mListItems.add((DATA++) + "");  
  
                mAdapter.notifyDataSetChanged();  
                mPullRefreshListView.onRefreshComplete();  
            }  
        }, 1000);  
    }  
}  

需要的activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent" >  
          
        <com.handmark.pulltorefresh.library.PullToRefreshListView  
            android:id="@+id/pull_refresh_list"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent"  
           
            android:divider="@android:color/black"  
            android:dividerHeight="1dip"  
              
            android:fastScrollEnabled="false"  
            android:footerDividersEnabled="false"  
            android:headerDividersEnabled="false"  
            android:smoothScrollbar="true" />  
    </RelativeLayout>  
技術QQ羣:364595326
發佈了10 篇原創文章 · 獲贊 6 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章