解决使用SwipeRefreshLayout ListView使用EmptyView冲突

ListView使用EmptyView的必要条件

listView.setEmptyView(view)方法中的view必须和listView属于同一个ViewGroup。负责即使添加了也不会起作用。

SwipeRefreshLayout下添加EmptyView,不能实现效果的原因

SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout)虽然是ViewGroup的子类,但是他只允许有一个子控件,所以在它下面同时添加ListView和EmptyView,EmptyView不能注册到SwipeRefreshLayout。

private View mTarget; // the target of the gesture
...
private void ensureTarget() {
        // Don't bother getting the parent height if the parent hasn't been laid
        // out yet.
        if (mTarget == null) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                if (!child.equals(mCircleView)) {
                    mTarget = child;
                    break;
                }
            }
        }
    }
SwipeRefreshLayout中添加FramLayout包含ListView和EmptyView存在的问题

这个方案能够在ListView为空的时候显示EmptyView,但是影响ListView的下拉(与下拉刷新空间冲突)。

通过代码添加EmptyView的问题

通过listView.getParent()可以获取到ListView的父控件,通过它的addView()可以添加EmptyView。但是当列表为空的时候就不能再下拉刷新了。

完善的解决方法

通过ViewGroup(不能是LinearLayout)下添加两个SwipeRefreshLayout,一个包含ListView,另外一个包含被ScrollView包含的EmptyView。第二个SwipeRefreshLayout作为EmptyView,需要ScrollView是因为需要他来接受下拉事件传递。
代码中,两个SwipeRefreshLayout最好是设置一样的样式和下拉监听。

布局文件和代码如下:

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tobrun.example.swipetorefresh.MainActivity">

    <ImageView
        android:id="@+id/igHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"/>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout_listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/igProgress">

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout_emptyView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/igProgress">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/emptyView"
                android:text="@string/empty"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:gravity="center" />

        </ScrollView>

    </android.support.v4.widget.SwipeRefreshLayout>


</RelativeLayout>

activity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ……此处省去findViewById()以及部分初始化

         initSwipeLayout(swipeLayout);
        initSwipeLayout(swipeLayoutEmpty);

        lv.setEmptyView(swipeLayoutEmpty);
        lv.setAdapter(adapter);

}

  private void initSwipeLayout(SwipeRefreshLayout layout) {
        layout.setColorSchemeResources(R.color.ics_blue_bright, R.color.ics_green_light,
                R.color.ics_orange_light, R.color.ics_red_light);
        layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                reloadData();
            }
        });
    }

    //此处使用的是AndroidAnnotations,意义就是使用了非UI线程
    @Background
    void reloadData() {
        // getData methods
        ......
        bindData();
    }

    //此处使用的是AndroidAnnotations,意义就是使用了UI线程,同runOnUIThread
    @UIThread
    void bindData() {
        // bindDataMethods
        .......
        swipeLayout.setRefreshing(false);
        swipeLayoutEmpty.setRefreshing(false);
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值