您的位置:首页 > 其它

优雅的解决SwipeRefreshLayout和ListView的EmptyView共存冲突的问题(全网独创)

2017-11-15 01:08 513 查看
故事背景

自从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> 其他的都不用变,这个布局非常巧妙的解决了以上难题,应该是全网最优雅的一种方式了,不敢独享,遂熬夜拿出来跟大家一起共享~~~~~ 

最后放上一张效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: