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

Android下拉和上拉刷新ListView列表内容的的一个优秀开源框架,在github上的项目链接地址:https://github.com/chrisbanes/Android-PullToRefresh

该PullToRefresh第三方控件功能强大,使用方便。
具体使用方法:

(1)首先到github上把该项目下载解压,导入到Eclipse的工程中。
(2)将Android-PullToRefresh作为一个lib引用到自己的项目中。
然后直接使用即可。

package zhangphil.pulltorefresh;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;@Overrideprotected 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>() {// 下拉@Overridepublic void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {Toast.makeText(getApplicationContext(), "下拉刷新",Toast.LENGTH_SHORT).show();addItem();}// 上拉@Overridepublic void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {Toast.makeText(getApplicationContext(), "上拉刷新",Toast.LENGTH_SHORT).show();addItem();}});// 列表到底,即看到最后一个元素。mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {@Overridepublic 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() {@Overridepublic void run() {mListItems.add((DATA++) + "");mAdapter.notifyDataSetChanged();mPullRefreshListView.onRefreshComplete();}}, 1000);}}
布局文件:

<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>


转载地址:http://blog.csdn.net/zhangphil/article/details/44647711


更多相关文章

  1. 【Android】Android清除本地数据缓存代码
  2. Android打开通讯录并获取数据
  3. 【Android Native Code开发系列】 五 运行、调试编译的Native Co
  4. Android发送数据到web服务器4种方式
  5. 全球支持最多运行平台的NoSQL数据库 iBoxDB
  6. Android Sqlite Failed to open database(无法打开数据库文件)
  7. 对android 项目工程 sdk编译版本、build工具版本等一系列版本统
  8. SharedPreferences之Android数据保存
  9. 基于Java LinkedList,实现Android大数据缓存策略

随机推荐

  1. android中文件IO
  2. Android横竖屏切换总结
  3. android 进程与线程 - 开发文档翻译 - 线
  4. 安卓中RelativeLayout布局
  5. android:20.活用Android的Message Queue(
  6. 菜鸟级的android程序员面试时候需要掌握
  7. android中的数据库操作
  8. Android性能调优工具TraceView介绍
  9. android单元测试初探——Instrumentation
  10. Android夸进程通信机制一:多进程简介