Android 带有空 TextView 的 SwipeRefreshLayout 无法正常工作

Android SwipeRefreshLayout with empty TextView not working properly

我想做的是,允许用户滑动和刷新列表,数据是否存在并不重要。但是当列表视图中有数据时,只有列表视图应该是可见的,而当数据不存在时,空文本视图应该是可见的。在这两种情况下,用户都必须能够通过滑动来刷新列表。

我尝试了一些给出的解决方案 here in this discussion but none of them sounds working, the idea of taking two SwipeToRefresh works fine as given Here,但即使从服务器获取数据时它也显示空容器。

我尝试用我自己的逻辑将 Listview 和 Textview 包装在 Relative/FrameLayout 中,但 SwipeToRefresh 仅根据其行为接受一个视图

这是我试过的xml片段

 <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/activity_main_swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.945" >


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

            <ListView
                android:id="@+id/event_list_eventlist"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:divider="@color/event_background"
                android:dividerHeight="1dp" >
            </ListView>

            <TextView
                android:id="@+id/event_txt_nothing_found"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerInParent="true"
                android:layout_centerVertical="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/darker_gray"
                android:visibility="gone" />
        </RelativeLayout>
    </android.support.v4.widget.SwipeRefreshLayout>

请帮忙

我用FrameLayout解决了同样的问题,看下面的代码:

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

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/layout_swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ListView
            android:id="@+id/lv_products"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@null" />

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

    <TextView
        android:id="@+id/tv_no_product"
        style="@android:style/TextAppearance.Medium"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:gravity="center"
        android:text="@string/msg_no_listing"
        android:textColor="@color/primary_green"
        android:visibility="gone" />
</FrameLayout>

您也可以使用 RelativeLayout,但关键是将 ListView 放在 SwipeRefreshLayout 中,而将 TextView 放在外面。

我也遇到了这个问题,并在 activity 中使用下面的布局解决了它 没有任何额外的代码。

如果您使用的是 ListActivityListFragment,它会为您处理 showing/hiding 空视图,刷新也适用于空列表以及此布局结构。

无需破解,一切都在 SwipeRefreshLayout 中,这是应该的。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Refresher"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@null" />
        <ScrollView
            android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:text="Geen formulieren gevonden"
                style="@style/text.EmptyView" />
        </ScrollView>
    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>

希望它能帮助任何正在解决这个问题的人。

最终使用以下代码解决了问题

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_shop_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <ScrollView
                android:gravity="center"
                android:fillViewport="true"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <com.forysta.inloyal.view.textviews.RegularTextView
                    android:id="@+id/tv_no_shop_data"
                    style="@style/text_Size_16"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="@string/alert_nodatashop"
                    android:visibility="visible"
                    app:layout_collapseMode="parallax" />
            </ScrollView>
        </LinearLayout>
    </android.support.v4.widget.SwipeRefreshLayout>

我的问题似乎没有记录,但如果回收器视图的适配器没有数据,并且您调用了 setLayoutManager 函数,则回收器视图变为 un-swipeable。我的解决方案是仅在收到一些数据后才调用 setLayoutManager