AppBarLayout中的SwipeRefreshLayout无法包裹内容

10

我正在尝试实现下拉刷新,但是我遇到了一个问题:SwipeRefreshLayout没有包裹子视图的高度。在视图预览和实际构建中,它的高度显示为0。

布局如下:

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="0dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:layout_collapseMode="pin">

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

            <include layout="@layout/child_layout" />

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

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

我还尝试将 SwipeRefreshLayout 设为 AppBarLayout 的父级,以及在 SwipeRefreshLayout 中放置一个单一的 LinearLayout,但都没有成功。唯一似乎有效的方法是静态地设置其高度,但我希望它基于子视图的高度是动态的。

这里是否有什么我错过的东西?似乎可能存在 SwipeRefreshLayout 的 bug,因为将其替换为同样包装内容的 LinearLayout 可按预期工作。


如果将SwipeRefreshLayout视图的“layout_height”更改为“match_parent”,会发生什么?只是随口一想... - Phix
3
дЅ иѓЅеЏ‘дёЂдё‹child_layoutзљ„д»Јз Ѓеђ—пјџ - n_r
你为什么把它放在工具栏里?为什么不单独制作? - Saveen
3个回答

6
问题在于你的SwipeRefreshLayout位于ToolbarAppBarLayout内部。你需要使用另一个布局包裹AppBarLayout,并将SwipeRefreshLayout放置在AppBarLayout下面。下面是一个示例。
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:fitsSystemWindows="true"
tools:context="com.vsahin.moneycim.View.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:fitsSystemWindows="true"
    app:expanded="false">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:background="@drawable/gradient_background">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways" />

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

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

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

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

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

我在原始帖子中忘记提到了,但是有一个Recycler View和底部工具栏在应用程序栏布局下面。问题是,当打开底部工具栏时,我需要应用程序栏布局的视差效果,但我只想支持在应用程序栏布局上进行下拉刷新。 - kira_codes
为什么你要固定AppBarLayout的高度,如果你将其更改为wrap_content,它将无法正常工作。 - Safeer
我想这样做是因为在我的原始代码中,我放了一个片段。在我的片段中,我使用自动缩放的文本视图来适应边界。@Safeer - VolkanSahin45
我认为问题出在折叠式工具栏是match parent,如果你使用apparlayout wrap content布局,就无法绘制布局。appbar将尝试wrap content,但collapsingtoolbar是match parent,它将尝试匹配其父级,但appbar没有固定的尺寸。@Safeer - VolkanSahin45
我被其他事情缠住了,刚回来看到这个,但你是对的。这个按预期工作。最初为什么它对我不起作用是因为我忘记了一些需要更新的编程行为逻辑。谢谢! - kira_codes

1
如果你的SwipeRefreshLayout高度包裹着child_layout的内容,那么你的child_layout高度应该设置为match_parent,或者同时将SwipeRefreshLayoutchild_layout的高度都设置为match_parent,如Android文档中所示。
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
      <!--Child View-->
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

-1

SwipeRefreshLayout 是一个透明的 View,所以最好的解决方案是将其设置为 match_parent,这样它就不会与其他视图发生冲突,并且不要在其中包含任何 ViewLayout,保持干净。

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

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="0dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:layout_collapseMode="pin">

        <include layout="@layout/child_layout" />

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

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

SwipeRefreshLayout 必须在顶部以覆盖其他层并可点击。


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