SwipeRefreshLayout是android官方的下拉刷新控件;
它内部有且只能有一个子控件;
当一个ListView嵌入到它内部时,就不能为ListView带一个EmptyView了;
于是很自然的想到将ListView和EmptyView纳入到一个父控件中;
典型的像下面这样的布局:
<android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipeRefreshLayout"        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="match_parent"                android:cacheColorHint="@android:color/transparent"                android:divider="@color/text_color_c4"                android:dividerHeight="@dimen/line_width"                android:footerDividersEnabled="true" />            <include                android:id="@+id/emptyView"                layout="@layout/empty_view_for_tang_friend_0" />        </FrameLayout>    </android.support.v4.widget.SwipeRefreshLayout>


这样就可以实现ListView和EmptyView都下拉刷新了;
但是问题来了,当ListView的数据超过一屏的时候,再往上滑,滑不上去了;
这就是SwipeRefreshLayout和ListView的EmptyView共存冲突的问题(如果SwipeRefreshLayout中仅有一个ListView的话没问题,但现在多了一个EmptyView);

解决办法有是有,虽然不那么优雅:
swipeRefreshLayout = (android.support.v4.widget.SwipeRefreshLayout) v.findViewById(R.id.swipeRefreshLayout);                swipeRefreshLayout.setColorSchemeResources(R.color.text_color_c6, R.color.yellow, R.color.grey, R.color.red);                swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()                {                    @Override                    public void onRefresh()                    {                        // TODO Auto-generated method stub                        //dosomething                    }                });listView = (ListView) v.findViewById(R.id.listView);                listView.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)                            swipeRefreshLayout.setEnabled(true);                        else                            swipeRefreshLayout.setEnabled(false);                    }                });

在ListView滑动的时候做一个判断,看看是不是滑动到顶部了,到顶部了就swipeRefreshLayout.setEnabled(true);让SwipeRefreshLayout可以接受动作,这样就ok了。


上面的解决方案比较简单,但是有个小问题,就是当listView滑动到第一个Item(让第一个Item显示一半)的时候,再上滑就上不去了,始终显示半个Item。
为了解决这个问题,只能使用笨办法了:将ListView和EmptyView分离,让他们两个分别被两个SwipeRefreshLayout包裹,如下面的布局:
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipeRefreshLayout"        android:layout_width="match_parent"        android:layout_height="match_parent" >        <cn.tangdada.tangbang.widget.LoadMoreListView            android:id="@+id/listView"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:cacheColorHint="@android:color/transparent"            android:divider="@color/text_color_c4"            android:dividerHeight="@dimen/line_width"            android:footerDividersEnabled="false" />    </android.support.v4.widget.SwipeRefreshLayout>    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipeRefreshLayoutEmptyView"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:visibility="gone" >        <TextView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:gravity="center"            android:paddingLeft="32dp"            android:paddingRight="32dp"            android:text="@string/empty"            android:textColor="@color/text_color_c3"            android:textSize="@dimen/font_big" />    </android.support.v4.widget.SwipeRefreshLayout></FrameLayout>


然后需要在代码中控制显示EmptyView的时机,比如我这里的:

private void updateViews(Object obj)    {        ArrayList<User> list = (ArrayList<User>) obj;        if (list.size() == 0)        {            //listView.removeLoadMoreListener();        }        users.addAll(list);        if (users.size() == 0)        {            swipeRefreshLayout.setVisibility(View.GONE);            swipeRefreshLayout.setRefreshing(false);            swipeRefreshLayoutEmptyView.setVisibility(View.VISIBLE);            swipeRefreshLayoutEmptyView.setRefreshing(false);        }        else        {            //listView.onLoadMoreComplete();            swipeRefreshLayout.setVisibility(View.VISIBLE);            swipeRefreshLayout.setRefreshing(false);            swipeRefreshLayoutEmptyView.setVisibility(View.GONE);            swipeRefreshLayoutEmptyView.setRefreshing(false);        }        notifyDataSetChanged();    }


SwipeRefreshLayout完美添加及完善上拉加载功能
http://blog.csdn.net/ljx19900116/article/details/41806863

android-Ultra-Pull-To-Refresh 源码解析
http://www.open-open.com/lib/view/open1436405920005.html

更多相关文章

  1. Android仿考拉全局滑动返回及联动效果的实现方法
  2. Android(安卓)Studio精彩案例(三)《模仿微信ViewPage+Fragment实
  3. Android(安卓)开发艺术探究V第三章之view的事件分发机制
  4. 从0系统学Android--3.2四种基本布局
  5. Layout___模块重用之include布局整合
  6. Android使用自己的字体库让你的APP更漂亮
  7. Android(安卓)Annotations 学习之路(一)环境配置以及@EActivity在
  8. android 测量控件视图的方法
  9. [置顶] android实现向右滑动返回功能

随机推荐

  1. Android(安卓)7.1.2(Android(安卓)N) And
  2. 2.4.11 网格视图和图像切换器
  3. android编译时添加库第三方库文件
  4. android编译时添加库第三方库文件
  5. Camera Flash的获得权限
  6. 2.4.10 可展开的列表组件
  7. 登录案例_android
  8. android 蓝牙
  9. 另类方法屏蔽Android4.03的HOME按键
  10. 代码中设置drawableright