优雅的解决SwipeRefreshLayout和ListView的EmptyView共存冲突的问题(全网独创)
来源:互联网 发布:动态恐怖漫画软件 编辑:程序博客网 时间:2024/05/17 15:41
故事背景
自从Android Material Design出来之后,以其优雅酷炫的效果深受广大用户的喜爱,我个人也非常喜欢其漂亮的界面,特别是SwipeRefreshLayout,呵呵看脸的世界ing
言归正传最近有个项目需要用到SwipeRefreshLayout嵌套一个listView,该listview不需要上拉加载,就是只需要简单的下拉刷新即可,于是二话不说,直接SwipeRefreshLayout嵌套ListView,嗯,效果完美,布局代码如下
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"/> </android.support.v4.widget.SwipeRefreshLayout>但是考虑到当listview没有数据的话应该显示一个提示,比如“暂无数据,下拉刷新”之类的提示,于是把布局改成下面这样:
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"/> <include layout="@layout/empty_layout"/> </android.support.v4.widget.SwipeRefreshLayout>
empty_layout.xml<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/empty_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="@string/no_content_refresh"/>
然后java代码中为ListView设置一下EmptyView,编译运行,当listview无数据的时候,发现设置无效!!!
网上查找资料说是SwipeRefreshLayout只能有一个子View,于是按照网上方法改成下面这样:
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"/> <include layout="@layout/empty_layout"/> </FrameLayout> </android.support.v4.widget.SwipeRefreshLayout>
更大的问题来了当SwipeRefreshLayout只有ListView一个子view的时候是没有任何问题的,但如果不是一个子view就会出现问题了,向上滑动ListView一切正常,向下滑动的时候就会出现还没有滑倒ListView顶部就触发下拉刷新的动作了。加了empty_layout之后listview根本无法下拉,每次下拉都是触发SwipeRefreshLayout的下拉方法。
于是参考了各种方案,如下
重写SwipeRefreshLayout http://www.eoeandroid.com/forum.php?mod=viewthread&tid=914273
设置listview的setOnScrollListener https://www.2cto.com/kf/201702/601860.html
以及设置onTouchListener
mListView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_MOVE) { Log.d("Measure", "listview.getListPaddingTop():"+mListView.getListPaddingTop()+ " listview.getTop():"+mListView.getTop()+"listview.getChildAt(0).getTop():"+mListView.getChildAt(0).getTop()); if (mListView.getFirstVisiblePosition() == 0 && mListView.getChildAt(0).getTop() >= mListView.getListPaddingTop()) { mRefreshLayout.setEnabled(true); Log.d("TAG", "reach top!!!"); }else mRefreshLayout.setEnabled(false); } return false; } });
发现设置onTouchListener和setOnScrollListener 效果差不多,listview确实能够下滑了,但是有一个小bug,好像SwipeRefreshLayout 下拉的时候有点不灵光,有时候需要下拉两三次才能够触发下拉刷新,log一直输出
Got ACTION_MOVE event but don't have an active pointer id
然后效果类似下面这样:
无法触发下拉,尝试了两三次之后,又正常了,这个效果还是不太满意,so,受到网友的启发,得到了下面的方案:
终极解决方案:
其实只需要把布局稍微改变一下即可完美解决,修改之后的布局如下:
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/empty_layout"/> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"/> </android.support.v4.widget.SwipeRefreshLayout> </FrameLayout>其他的都不用变,这个布局非常巧妙的解决了以上难题,应该是全网最优雅的一种方式了,不敢独享,遂熬夜拿出来跟大家一起共享~~~~~
最后放上一张效果图
阅读全文
0 0
- 优雅的解决SwipeRefreshLayout和ListView的EmptyView共存冲突的问题(全网独创)
- SwipeRefreshLayout和ListView的EmptyView共存冲突的问题
- SwipeRefreshLayout和ListView的EmptyView共存冲突的问题
- 解决SwipeRefreshLayout结合ListView EmptyView使用不起作用的问题
- 判断Listview滑到顶部的最精准方案,解决Listview设置EmptyView与SwipeRefreshLayout冲突
- Android的headerView和emptyView共存问题
- Android 解决ListView 和 ScrollView 共存冲突的问题
- Android 解决ListView 和 ScrollView 共存冲突的问题
- Android 解决ListView 和 ScrollView 共存冲突的问题
- Android 解决ListView 和 ScrollView 共存冲突的问题
- Android 解决ListView 和 ScrollView 共存冲突的问题
- Android 解决ListView 和 ScrollView 共存冲突的问题
- Android 解决ListView 和 ScrollView 共存冲突的问题
- Android 解决ListView 和 ScrollView 共存冲突的问题
- 关于处理解决ListView 和 ScrollView 共存冲突的问题。
- Android 解决ListView 和 ScrollView 共存冲突的问题
- Android 解决ListView 和 ScrollView 共存冲突的问题
- Android 解决ListView 和 ScrollView 共存冲突的问题
- HashMap 源码分析 -- entrySet()
- springcloud和dubbo微服务架构图
- python学习笔记——字符串操作
- 深夜沐月幻想乡——逝月镜夜【前篇】
- Spring Security 中实现 Remember Me 记住密码功能
- 优雅的解决SwipeRefreshLayout和ListView的EmptyView共存冲突的问题(全网独创)
- 去掉控制台程序的黑框框
- Swift4.0实现简单通讯录功能,FMDB框架实现联系人的增删改查
- 分析C语言声明
- Poj 3279 Fliptile(反转/开关问题)
- java线程安全篇之synchronized 多个线程多个锁(二)
- 小程序中的小问题
- hdu 1534 Schedule Problem
- Sending Packets LightOJ