Android开源--PullToRefresh
开源地址: https://github.com/chrisbanes/Android-PullToRefresh
简单介绍:PullToRefresh是一款支持ListView,GridView,ViewPager,ScrollView,WebView等一切能够拖动,并实现上下左右拖动刷新数据的框架,废话不多说,上代码;
[主要的android依赖项目存放在library中,支持fragment列表,ViewPager列表的存放在extras包中]
[依赖项目导入如左图]
1.支持ListView的实现:
1)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" > <com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/my_ptr_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#0000"
android:divider="#0FF"
android:dividerHeight="1dp"
android:smoothScrollbar="true"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" /> </RelativeLayout>
2)JAVA代码
public class PullListSampleActivity extends Activity { private PullToRefreshListView mPullToRefreshLv;
private ArrayAdapter<String> mAdapter;
private LinkedList<String> mListItems; private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
"Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
"Allgauer Emmentaler" }; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pull_list_sample);
//填充数据
mPullToRefreshLv=(PullToRefreshListView) findViewById(R.id.my_ptr_lv);
mListItems=new LinkedList<String>(Arrays.asList(mStrings));
mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,mListItems);
mPullToRefreshLv.setAdapter(mAdapter);
//下拉刷新的声音支持
SoundPullEventListener<ListView> soundPullEventListener=new SoundPullEventListener<ListView>(this);
soundPullEventListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
soundPullEventListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
soundPullEventListener.addSoundEvent(State.RESET, R.raw.reset_sound);
mPullToRefreshLv.setOnPullEventListener(soundPullEventListener);
//刷新时调用的监听器
mPullToRefreshLv.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) { String label=DateUtils.formatDateTime(PullListSampleActivity.this, System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
//设置头部Label
mPullToRefreshLv.getLoadingLayoutProxy().setLastUpdatedLabel(label);
new GetListDataTask().execute();
} }); mPullToRefreshLv.setOnLastItemVisibleListener(new OnLastItemVisibleListener() { @Override
public void onLastItemVisible() {
Toast.makeText(PullListSampleActivity.this,"all data loaded !", 800).show(); } });
} class GetListDataTask extends AsyncTask<Void, Void, String[]>{ @Override
protected String[] doInBackground(Void... params) {
//mock get datas
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return mStrings;
} @Override
protected void onPostExecute(String[] result) {
mListItems.addAll(Arrays.asList(result));
mAdapter.notifyDataSetChanged();
//手动关闭头部
mPullToRefreshLv.onRefreshComplete(); super.onPostExecute(result);
} }
}
2.支持GridView
1)XML实现
ptr:ptrMode="both"表示上下/左右都能够刷新
ptr:ptrDrawable表示刷新时显示的图标
<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" > <com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_ptr_gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#0000"
android:divider="#0FF"
android:dividerHeight="1dp"
android:smoothScrollbar="true"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:numColumns="auto_fit"
ptr:ptrMode="both"
ptr:ptrDrawable="@drawable/default_ptr_rotate" /> </RelativeLayout>
2)JAVA代码实现
public class PullGridSampleActivity extends Activity { private PullToRefreshGridView mPullToRefreshGridView;
private ArrayAdapter<String> mAdapter;
private LinkedList<String> mListItems;
private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
"Allgauer Emmentaler" }; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pull_grid_sample); mPullToRefreshGridView=(PullToRefreshGridView) findViewById(R.id.my_ptr_gv);
mListItems=new LinkedList<String>();
mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
android.R.id.text1,mListItems);
mPullToRefreshGridView.setAdapter(mAdapter); //支持当没有数据的时候替代的View 详细代码依据自己的业务
TextView textView=new TextView(this);
LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
params.gravity=Gravity.CENTER;
textView.setLayoutParams(params);
textView.setText("The data is empty !");
mPullToRefreshGridView.setEmptyView(textView); SoundPullEventListener<GridView> pullEventListener=new SoundPullEventListener<GridView>(this);
pullEventListener.addSoundEvent(State.PULL_TO_REFRESH,R.raw.pull_event);
pullEventListener.addSoundEvent(State.RESET,R.raw.reset_sound);
pullEventListener.addSoundEvent(State.REFRESHING,R.raw.refreshing_sound);
mPullToRefreshGridView.setOnPullEventListener(pullEventListener); mPullToRefreshGridView.setOnRefreshListener(new OnRefreshListener2<GridView>() { @Override
public void onPullDownToRefresh(
PullToRefreshBase<GridView> refreshView) {
String label=DateUtils.formatDateTime(getApplicationContext(),
System.currentTimeMillis(),DateUtils.FORMAT_24HOUR);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
new GetDataTask().execute();
} @Override
public void onPullUpToRefresh(
PullToRefreshBase<GridView> refreshView) {
String label=DateUtils.formatDateTime(getApplicationContext(),
System.currentTimeMillis(),DateUtils.FORMAT_24HOUR);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
new GetDataTask().execute();
} }); } class GetDataTask extends AsyncTask<Void, Void, String[]>{ @Override
protected String[] doInBackground(Void... params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return mStrings;
} @Override
protected void onPostExecute(String[] result) {
mListItems.addAll(Arrays.asList(result));
mAdapter.notifyDataSetChanged(); mPullToRefreshGridView.onRefreshComplete(); } } }
3.对ViewPager的实现
1)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" > <com.handmark.pulltorefresh.extras.viewpager.PullToRefreshViewPager
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_ptr_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptrAnimationStyle="flip"
ptr:ptrMode="both"
ptr:ptrHeaderBackground="#FFFF" /> </RelativeLayout>
2)Java实现
public class PullViewPagerSample extends Activity implements OnRefreshListener<ViewPager>{ private PullToRefreshViewPager mPullToRefreshViewPager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pull_viewpager_sample); mPullToRefreshViewPager=(PullToRefreshViewPager) findViewById(R.id.my_ptr_viewpager); mPullToRefreshViewPager.getRefreshableView().setAdapter(new MyPagerAdapter());
mPullToRefreshViewPager.setOnRefreshListener(this); } class MyPagerAdapter extends PagerAdapter{ private final int[] sDrawables = { R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper,
R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper }; @Override
public int getCount() {
return sDrawables.length;
} @Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(container.getContext());
imageView.setImageResource(sDrawables[position]);
container.addView(imageView,LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
return imageView;
} @Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==(View)arg1;
} @Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
} } @Override
public void onRefresh(PullToRefreshBase<ViewPager> refreshView) {
try {
Thread.sleep(4000);//模拟载入时间
} catch (InterruptedException e) {
e.printStackTrace();
}
mPullToRefreshViewPager.onRefreshComplete();
} }
Android开源--PullToRefresh的更多相关文章
- Android开源项目pulltorefresh分析与简单使用
在Android开发中有时我们须要訪问网络实时刷新数据.比方QQ好友在线状态最新信息,QQ空间须要显示很多其它的好友动态信息,EOE论坛client显示很多其它的文章帖子信息等.android-pul ...
- 2015-2016最火的Android开源项目--github开源项目集锦(不看你就out了)
标签: Android开发开源项目最火Android项目github 2015-2016最火的Android开源项目 本文整理与集结了近期github上使用最广泛最火热与最流行的开源项目,想要充电与提 ...
- Android开源项目分类汇总
目前包括: Android开源项目第一篇——个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...
- 59.Android开源项目及库 (转)
转载 : https://github.com/Tim9Liu9/TimLiu-Android?hmsr=toutiao.io&utm_medium=toutiao.io&utm_so ...
- Android 开源项目分类汇总(转)
Android 开源项目分类汇总(转) ## 第一部分 个性化控件(View)主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Galler ...
- Android 开源项目分类汇总
Android 开源项目分类汇总 Android 开源项目第一篇——个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView ...
- Android开源项目及库搜集
TimLiu-Android 自己总结的Android开源项目及库. github排名 https://github.com/trending,github搜索:https://github.com/ ...
- material design 的android开源代码整理
material design 的android开源代码整理 1 android (material design 效果的代码库) 地址请点击:MaterialDesignLibrary 效果: 2 ...
- Android开源项目库汇总
最近做了一个Android开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个star. 抽 ...
随机推荐
- logrotate 学习使用
logrotate Summary : Rotates, compresses, removes and mails system log files Description : The logrot ...
- TP5使用技巧
1.fetchSql用于直接返回SQL而不是执行查询,适用于任何的CURD操作方法. 例如:$result = Db::table('think_user')->fetchSql(true)-& ...
- C/C++/C#程序如何打成DLL动态库
C/C++程序如何打成DLL动态库:1.在VS中新建main.h,添加如下内容:extern "C" _declspec(dllexport) int onLoad(); 2.新建 ...
- 使用sqlplus执行sql时,发现有中文有乱码解决方法
https://blog.csdn.net/fyyinjing/article/details/77877239
- 大牛教你如何循序渐进,有效的学习JavaScript?
首先要说明的是,咱现在不是高手,最多还是一个半桶水,算是入了JS的门.谈不上经验,都是一些教训. 这个时候有人要说,“靠,你丫半桶水,凭啥教我们”.您先别急着骂,先听我说! 你叫一个大学生去教小学数学 ...
- 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
- 洛谷 P1060 开心的金明【DP/01背包】
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就 ...
- Oracle remove duplicate
DELETE FROM your_table WHERE rowid not in (SELECT MIN(rowid) FROM your_table GROUP BY column1, colum ...
- [POI2015]Kinoman
题目大意: 给你一个长度为n的数列f,f中共有m种不同的数,每种数都有一个权值w[i]. 你可以选定一个f中的区间,定义区间的权值为这一区间只出现一次的数的权值和. 问权值最大的区间的权值是多少? 思 ...
- tiny4412 串口驱动分析八 --- log打印的几个阶段之内核启动阶段(printk tiny4412串口驱动的注册)
作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...