SwipeRefreshLayout和ListView的EmptyView共存冲突的问题

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

猜你喜欢

转载自gundumw100.iteye.com/blog/2177595