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

9
我想要做的是允许用户滑动并刷新列表,无论数据是否存在。但仅当ListView中有数据时,ListView应该可见;当没有数据时,Empty TextView 应该可见。在这两种情况下,用户都必须能够通过滑动来刷新列表。
我尝试了一些提供的解决方案,如此讨论中所述,但它们都没有起作用。两个 SwipeToRefresh 的想法可以像这里所示那样很好地工作,但即使从服务器获取数据,它也显示空容器。
我尝试过自己的逻辑,将 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>

请帮忙

4个回答

13

我通过使用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放在外面。


1
当列表为空时,您可以再次滑动。我确信它百分之百有效。 - Phuc Tran
请问您能否提供一些活动代码吗? - Bhavik Mehta
你尝试过上面的布局吗? - Phuc Tran
4
问题在于,如果将tv_no_product设置为空视图ListView,则当emptyView可见时,ListView会将其可见性设置为GONE,并且当ListView处于GONE状态时,您无法进行下拉刷新。 - BladeCoder
1
正如@BladeCoder所说,使用该解决方案无法刷新空视图。解决方案是将包含ListView和EmptyView的FrameLayout放置在SwipeRefreshLayout中。此外,在FrameLayout中设置android:clickable =“true”。 - antoinem
显示剩余8条评论

12

我也遇到了这个问题,通过使用下面的布局解决了它,不需要在活动中添加任何额外的代码

如果你正在使用 ListActivityListFragment,它会自动处理显示/隐藏空视图,并且使用这种布局结构,刷新同样可以用于空列表。

无需任何黑科技,一切都在 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>

希望这可以帮助任何遇到此问题的人。


太好了,它运行正常。我想知道为什么?有没有对此的解释? - GVillani82

1

我的问题似乎没有文档记录,但如果一个回收视图的适配器没有数据,并且您已经调用了setLayoutManager函数,那么回收视图就变得无法滑动。我的解决方案是仅在接收到一些数据后才调用setLayoutManager


0
最终使用以下代码解决了该问题。
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接