Java SwipeRefreshLayout无法使用空列表

Java SwipeRefreshLayout无法使用空列表,java,android,Java,Android,我的应用程序包含一个SwipeRefreshLayout,其中包含一个listView。列表从服务器接收数据。因此,当获取数据并正确填充列表时,滑动以刷新工作,但当数据未传递到列表且为空时,滑动刷新不工作。为什么listView为空时不能滑动 这就是我的布局: <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://

我的应用程序包含一个SwipeRefreshLayout,其中包含一个listView。列表从服务器接收数据。因此,当获取数据并正确填充列表时,滑动以刷新工作,但当数据未传递到列表且为空时,滑动刷新不工作。为什么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和滑动刷新布局位于嵌套滚动视图内,这意味着嵌套滚动视图的子级高度将自动变为换行内容(在您的情况下为相对布局)(它也可能会向您显示lint警告,以替换相对布局与换行内容的匹配父级高度)。 因此,当列表中没有数据时,整个布局组的高度将变为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>

>

啊,实际上这是一个很好的建议,谢谢!但是为了让它能正确使用我的折叠工具栏,我想我不能在嵌套的scrollview上放置布局,可以吗?好的!尝试将其放入嵌套的滚动视图android:layout\u gravity=“fill\u vertical”android:fillViewport=“true”好的,我必须添加两行,但不幸的是,我仍然存在相同的问题:当列表为空时,无法像那样重新加载它:/I已更新答案,我将整个布局包装在滑动刷新布局中,这样,无论listview大小如何,您的滑动区域都将覆盖整个屏幕,就像它工作了一半一样:可以这样刷新,但无法将列表回滚