簡體   English   中英

SwipeRefreshLayout不能與空列表一起使用

[英]SwipeRefreshLayout not working with empty List

我的應用程序包含一個SwipeRefreshLayout及其內部的listView。 該列表從服務器接收數據。 因此,當獲取數據並正確填充列表時,滑動刷新即可工作,但是當數據未傳遞到列表且為空時,SwipeRefresh將不起作用。 為什么在listView為空時我不能滑動?

那是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorGrayHell"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="140dp"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:expandedTitleMarginBottom="56dp"
        app:contentScrim="@color/colorPrimary"
        app:expandedTitleGravity="bottom|center"
        app:expandedTitleTextAppearance="@style/CollBar"
        app:title="Tankstellen"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:toolbarId="@+id/toolbar">

        <ImageView
            android:id="@+id/backg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@drawable/backthree"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_marginBottom="48dp"
            app:layout_collapseMode="pin"
            android:layout_gravity="bottom"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="55dp">

        <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="7dp"
                android:layout_marginRight="7dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/white"
                android:divider="@color/colorGrayHell"
                android:dividerHeight="10dp"
                android:nestedScrollingEnabled="true" />

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

    </RelativeLayout>

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

</android.support.design.widget.CoordinatorLayout>

您的listview和滑動刷新布局位於嵌套滾動視圖內,這意味着嵌套滾動視圖的子級高度自動變為換行內容(在您的情況下為相對布局)(它可能還會向您顯示棉絨警告,以替換匹配的父項您要包裝內容的相對布局的高度)。 因此,當列表中沒有數據時,整個布局組的高度將變為0,而需要滑動刷新的位置的高度將變為0,並且無法在高度為0的布局中滑動。

為了解決這個問題,您可以在嵌套滾動視圖上方采用線性布局,其高度為match parent,並將滑動刷新布局置於其上方。

讓我知道您是否需要示例代碼。

編輯

好的! 所以這是我發現可以解決此問題的解決方法。 您可以嘗試一下,如果能解決您的問題,請告訴我。 想法是將所有內容包裝為滑動刷新布局。

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">

  <android.support.v7.widget.LinearLayoutCompat
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent"
    android:background="@color/colorGrayHell"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:expandedTitleMarginBottom="56dp"
            app:contentScrim="@color/colorPrimary"
            app:expandedTitleGravity="bottom|center"
            app:expandedTitleTextAppearance="@style/CollBar"
            app:title="Tankstellen"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <ImageView
                android:id="@+id/backg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/backthree"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_marginBottom="48dp"
                app:layout_collapseMode="pin"
                android:layout_gravity="bottom"
                app:popupTheme="@style/AppTheme.PopupOverlay">

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="55dp">


                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="7dp"
                    android:layout_marginRight="7dp"
                    android:layout_marginTop="10dp"
                    android:background="@android:color/white"
                    android:divider="@color/colorGrayHell"
                    android:dividerHeight="10dp"
                    android:nestedScrollingEnabled="true" />


        </RelativeLayout>

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

    </android.support.design.widget.CoordinatorLayout>
  </android.support.v7.widget.LinearLayoutCompat>
   </android.support.v4.widget.SwipeRefreshLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM