android - AppBarLayout 中的 SwipeRefreshLayout 不包装内容

标签 android android-layout android-appbarlayout swiperefreshlayout

我正在尝试实现拉动刷新,但我遇到了 SwipeRefreshLayout 未包装 subview 高度的问题。在 View 预览和实时构建中,它的高度似乎为 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。唯一阻止滑动布局高度为 0 的方法是静态设置它,但我希望它根据 subview 的高度动态设置。

我在这里缺少什么吗? SwipeRefreshLayout 似乎存在错误,因为将其替换为同样按预期方式包装内容的 LinearLayout

最佳答案

问题是您的 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>

关于android - AppBarLayout 中的 SwipeRefreshLayout 不包装内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47801262/

相关文章:

android - 单击菜单按钮时 java.lang.RuntimeException : Expecting menu, 出现relativelayout错误

android - CoordinatorLayout 在滚动端折叠或展开 AppBar

android twitter4j oauth 在 3g 上不起作用

android - 在 Fragment 实现之后,有哪些选项可以保持 Android 1.5 的兼容性

android - 长 Android TextView 将其他 View 推离屏幕

android - 理解weight和layout_weight,layout_weight越大,在layout中缩小的越多。

android - CoordinatorLayout + ActionBar + fragment

android - 收听 AppBarLayout 的偏移量变化(折叠/展开)以更改 Kotlin 中工具栏的背景

java - 以编程方式连接到 wifi 网络(首先访问 ssid)?

android - ConstraintLayout 可以帮助将右侧项目流到下一行吗?