【问题标题】:Android SwipeRefreshLayout with empty TextView not working properly带有空 TextView 的 Android SwipeRefreshLayout 无法正常工作
【发布时间】:2015-06-13 11:22:09
【问题描述】:

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

我尝试了一些解决方案 here in this discussion 但它们听起来都不起作用,采用两个 SwipeToRefresh 的想法可以正常工作,因为给定Here,但即使在从服务器获取数据时它也会显示空容器。

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

这是我尝试过的 xml sn-p

 <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>

请帮忙

【问题讨论】:

    标签: android android-listview swiperefreshlayout


    【解决方案1】:

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

    【讨论】:

    • 在活动中做什么?我的意思是,如果列表为空,此实现是否允许再次滑动?
    • 当然,如果列表为空,您可以再次滑动。我确信它 100% 有效。
    • 如果可能的话,你能放一些代码吗?
    • 问题是如果你为ListView设置了tv_no_product为emptyView,那么ListView在emptyView可见的情况下将其可见性设置为GONE,当ListView为GONE时你不能滑动刷新。
    • 正如@BladeCoder 所说的那样,您无法刷新空视图。解决方案是将FrameLayout(包括ListViewEmptyView)放在SwipeRefreshLayout 中。另外,在FrameLayout中设置android:clickable=”true”
    【解决方案2】:

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

    如果您使用的是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>
    

    希望它能帮助任何在这个问题上苦苦挣扎的人。

    【讨论】:

    • 太好了,效果很好。我想知道为什么?对此有什么解释吗?
    【解决方案3】:

    我的问题似乎没有记录,但如果回收器视图的适配器没有数据,并且您调用了setLayoutManager 函数,那么回收器视图将变为不可滑动。我的解决方案是在收到一些数据后才调用setLayoutManager

    【讨论】:

      【解决方案4】:

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

      <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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-02
        • 2012-09-18
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        相关资源
        最近更新 更多