PullToRefreshListView的使用
2016-05-08 21:54
316 查看
PullToRefreshListView是Android-PullToRefresh开源项目中的一个自定义组件,Android-PullToRefresh是一个强大的拉动刷新开源项目,支持各种控件下拉刷新,ListView、ViewPager、WebView、ExpandableListView、GridView、ScrollView、Horizontal ScrollView、Fragment 上下左右拉动刷新。
今天就讲解PullToRefreshListView的使用,其他的组件会在接下来的博客中讲解。
开源地址:https://github.com/chrisbanes/Android-PullToRefresh
下载源码后,打开项目,如图
将上图的library导入Eclipse中,然后关联你的 项目,如果不会关联的话,可以到这里去看看/article/7626679.html
好了,一切工作准备好了。
布局文件
xmlns:ptr=”http://schemas.android.com/apk/res-auto”是必须的,只有这样ptr:ptrHeaderTextColor才可以用,还有其他属性,可以在library中的attrs.xml中找到,其他属性没有什么解释的。如图
MainActivity文件代码,先全部贴出。
上述代码已经注释了,应该看懂是不难的。
测试效果图:
刷新前
好了,PullToRefreshListView的使用学会了,有什么疑问可以留言。
今天就讲解PullToRefreshListView的使用,其他的组件会在接下来的博客中讲解。
开源地址:https://github.com/chrisbanes/Android-PullToRefresh
下载源码后,打开项目,如图
将上图的library导入Eclipse中,然后关联你的 项目,如果不会关联的话,可以到这里去看看/article/7626679.html
好了,一切工作准备好了。
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ptr="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#000000" > <!-- 上述的xmlns:ptr="http://schemas.android.com/apk/res-auto"是必须的,下面的属性才可以用 --> <!-- 替代ListView --> <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="#ffffff" android:dividerHeight="1dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true" ptr:ptrHeaderTextColor="#ffffff" /> </LinearLayout>
xmlns:ptr=”http://schemas.android.com/apk/res-auto”是必须的,只有这样ptr:ptrHeaderTextColor才可以用,还有其他属性,可以在library中的attrs.xml中找到,其他属性没有什么解释的。如图
MainActivity文件代码,先全部贴出。
package com.zengfeng.pulltorefreshlistviewdemo; import java.util.Arrays; 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.OnRefreshListener; import com.handmark.pulltorefresh.library.PullToRefreshListView; import android.app.ListActivity; import android.os.AsyncTask; import android.os.Bundle; import android.text.format.DateUtils; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends ListActivity { //数据 private LinkedList<String> mListItems; //更新布局listView private PullToRefreshListView mPullRefreshListView; //适配器 private ArrayAdapter<String> madapter; //数据 private String[] mListItem={"Android","Java","Phthy","C#","PHP"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化mPullRefreshListView mPullRefreshListView=(PullToRefreshListView) findViewById(R.id.pull_refresh_list); //设置刷新更新监听方式 mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override //跟新方法 public void onRefresh(PullToRefreshBase<ListView> refreshView) { //设置下拉时的时间显示 String label=DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); //更新label显示 refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); //进行异步更新 new GetDataTask().execute(); } }); //设置滑动尾部的监听器 mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() { @Override public void onLastItemVisible() { //滑动到底部是,提示已经到底部了 Toast.makeText(MainActivity.this, "已经滑动到底部了", Toast.LENGTH_LONG).show(); } }); //刷新时是否允许滑动 //mPullRefreshListView.isScrollingWhileRefreshingEnabled(); //在刷新时允许继续滑动 mPullRefreshListView.setScrollingWhileRefreshingEnabled(true); //得到模式,有三种,分别是Mode.PULL_FROM_START,Mode.BOTH,PULL_FROM_END //mPullRefreshListView.getMode(); //设置上下都可以刷新 mPullRefreshListView.setMode(Mode.BOTH); //得到刷新的listView ListView actualListView=mPullRefreshListView.getRefreshableView(); mListItems=new LinkedList<>(); mListItems.addAll(Arrays.asList(mListItem)); madapter=new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, mListItems); //设置适配器,下面两个效果相同 //mPullRefreshListView.setAdapter(madapter); actualListView.setAdapter(madapter); } private class GetDataTask extends AsyncTask<Void, Void, Void>{ @Override //后台执行任务 protected Void doInBackground(Void... params) { try { Thread.sleep(1000); } catch (Exception e) { } return null; } @Override protected void onPostExecute(Void result) { //得到刷新当前模式 Mode mode=mPullRefreshListView.getCurrentMode(); if(mode==Mode.PULL_FROM_START){ //如果模式是开头刷新效果 mListItems.addFirst("START Add"); }else{ //如果模式是尾部刷新效果 mListItems.addLast("Later Add"); } //通知数据已经改变了 madapter.notifyDataSetChanged(); //加载完以后停止刷新 mPullRefreshListView.onRefreshComplete(); super.onPostExecute(result); } } }
上述代码已经注释了,应该看懂是不难的。
测试效果图:
刷新前
好了,PullToRefreshListView的使用学会了,有什么疑问可以留言。
相关文章推荐
- 写了 35 年代码的老程序员的最大遗憾
- IC卡和ID卡的区别以及相同之处各是什么?
- Python 正则表达式
- fdisk -l 显示信息详解
- 20145330《Java程序设计》第五次实验报告
- 20150330--Dede+输出同栏目资源,评分
- 20145213《Java程序设计》第十周学习总结
- CSS的line-height属性
- 图论-最短路-BF算法
- yum默认软件安装路径
- Qt5解析json文件
- SQL Server 分组后取Top N
- Linux中线程与CPU核的绑定
- poj 2387 Til the Cows Come Home -- 最短路dijstra
- 三级菜单制作
- Linux 25周年:与创始人Linus Torvalds对话
- 20145205《Java程序设计》第四次实验:Android环境搭建
- Shell脚本编程入门到放弃
- 347. Top K Frequent Elements
- IBM Watson物联网平台的两个MQTT工具