带风字的诗句,山行的诗意,音乐家李天慧
android swiperefreshlayout下拉刷新
我们都知道现在android5.0以后就提倡使用material design设计了。在material design设计就有一个非常好的设计swiperefreshlayout,下面我们就来看看它的使用。既然它来源于material design,我们第一步就应该是添加它的库。
1、我们就在build.gradle添加库:
compile 'com.android.support:support-v4:22.1.1'
2、然后我们就直接在res/layouts/activity_main.xml布局里面使用:
<android.support.v4.widget.swiperefreshlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/id_swipe_refresh" android:layout_width="match_parent" android:layout_height="wrap_content"> <listview android:id="@+id/id_listview" android:layout_width="match_parent" android:layout_height="match_parent"></listview> </android.support.v4.widget.swiperefreshlayout>
我们可以看到swiperefreshlayout作为listview的父布局,当滑动到listview的边界时,swiperefreshlayout就会显示正在刷新的动画,同时会提供一个onrefresh的事件供我们加载数据。
3、提供数据源
这里我们直接用arrayadapter就行了,所以我们直接来定义string-array就行了。
<string-array name="singer_names"> <item>周杰伦</item> <item>那英</item> <item>刘德华</item> <item>张学友</item> <item>许巍</item> <item>朴树</item> <item>陈奕迅</item> <item>a_lin</item> <item>杨宗纬</item> </string-array>
4、设置adapter
setcontentview(r.layout.activity_main); mswiperefreshlayout = (swiperefreshlayout) findviewbyid(r.id.id_swipe_refresh); mlistview =(listview)findviewbyid(r.id.id_listview); string[] singer = getresources().getstringarray(r.array.singer_names); madapter = new arrayadapter<>(this, android.r.layout.simple_list_item_1, singer); mlistview.setadapter((listadapter) madapter); mswiperefreshlayout.setonrefreshlistener(new swiperefreshlayout.onrefreshlistener() { @override public void onrefresh() { refreshcontent(); } });
private void refreshcontent(){ new handler().postdelayed(new runnable() { @override public void run() { madapter = new arrayadapter<>(mainactivity.this, android.r.layout.simple_list_item_1, getsingernames()); mlistview.setadapter((listadapter) madapter); //设置刷新加载效果的icon是否继续显示 mswiperefreshlayout.setrefreshing(false); } },2000); } private list<string> getsingernames() { list<string> newcatnames = new arraylist<string>(); for (int i = 0; i < msingernames.length; i++) { int randomcatnameindex = new random().nextint(msingernames.length - 1); newcatnames.add(msingernames[randomcatnameindex]); } return newcatnames; }
主要是实现swiperefreshlayout.onrefreshlistener接口,然后实现onrefresh就可以刷新数据了,然后通过刷新数据源就可以更新数据了。其实用起来还是很简单的。
我们再来看看swiperefreshlayout的其他属性。
setcolorschemeresources(r.color.orange, r.color.green, r.color.blue); 改变加载图标的颜色。这样swiperefreshlayout旋转的时候将会在这三种颜色间切换
setenabled(false)禁止使用刷新通知
这个属性在一个地方可能会用到,那就是swiperefreshlayout包含多个childview的时候,有一个滑动事件冲突的问题,listview只能上滑,而不能下拉。一旦下拉,就会触发swiperefreshlayout的下拉刷新。这种情况肯定是在事件派发上出了问题。下拉的事件在通常情况下应该由listview来进行处理;当listview位于顶部时,由swiperefreshlayout来进行处理。而现在的情况是全都由swiperefreshlayout来处理的。这个问题有两种解决的办法:
1、我们知道这个是因为滑动派发的问题,那我们可以自定义一个swiperefreshlayout继承的improvedswipelayout;
在values文件夹中新建一个attrs.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="improvedswipelayoutattrs"> <attr name="scrollablechildid" format="reference" /> </declare-styleable> </resources>
在使用自定义view中指定listview的id:
<com.goach.palm.demo.improvedswipelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:fab="http://schemas.android.com/apk/res-auto" xmlns:isl="http://schemas.android.com/apk/res-auto" android:id="@+id/swipe_container" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/md_blue_grey_50" isl:scrollablechildid="@+id/list_statuses" > <framelayout android:layout_width="match_parent" android:layout_height="match_parent"> <listview android:id="@+id/list_statuses" android:minheight="?android:attr/listpreferreditemheight" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingtop="12dp" android:paddingbottom="12dp" android:paddingleft="8dp" android:paddingright="8dp" android:cliptopadding="false" android:divider="@android:color/transparent" android:dividerheight="12dp"/> <textview android:layout_width="match_parent" android:layout_height="40dp" android:text="2234544543" /> </framelayout> </com.goach.palm.demo.improvedswipelayout>
最后是我的improvedswipelayout全部代码:
public class improvedswipelayout extends swiperefreshlayout { private static final string tag = improvedswipelayout.class.getcanonicalname(); private int mscrollablechildid; private view mscrollablechild; public improvedswipelayout(context context) { this(context, null); } public improvedswipelayout(context context, attributeset attrs) { super(context, attrs); typedarray a = context.obtainstyledattributes( attrs, r.styleable.improvedswipelayoutattrs); mscrollablechildid = a.getresourceid(r.styleable.improvedswipelayoutattrs_scrollablechildid, 0); mscrollablechild = findviewbyid(mscrollablechildid); a.recycle(); } @override public boolean canchildscrollup() { ensurescrollablechild(); if (android.os.build.version.sdk_int < 14) { if (mscrollablechild instanceof abslistview) { final abslistview abslistview = (abslistview) mscrollablechild; return abslistview.getchildcount() > 0 && (abslistview.getfirstvisibleposition() > 0 || abslistview.getchildat(0) .gettop() < abslistview.getpaddingtop()); } else { return mscrollablechild.getscrolly() > 0; } } else { return viewcompat.canscrollvertically(mscrollablechild, -1); } } private void ensurescrollablechild() { if (mscrollablechild == null) { mscrollablechild = findviewbyid(mscrollablechildid); } } }
还有一种方法就是我们使用上面的setenabled来实现,通过listview的onscrolllistener来实现,当滑动到第一个可见的item为0的时候,我们就setenabled(true),否则反之。
lview.setonscrolllistener(new abslistview.onscrolllistener() { @override public void onscrollstatechanged(abslistview abslistview, int i) { } @override public void onscroll(abslistview abslistview, int firstvisibleitem, int visibleitemcount, int totalitemcount) { if (firstvisibleitem == 0) swipeview.setenabled(true); else swipeview.setenabled(false); } });
这样,就可以很好的解决这个问题了。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio开发小型对话机器人app(实例代码)
Android通过Java sdk的方式接入OpenCv的方法
Android 通过cmake的方式接入opencv的方法步骤
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
Android 进度条 ProgressBar的实现代码(隐藏、出现、加载进度)
网友评论