AppBarLayout中的SwipeRefreshLayout未包装内容

问题描述 投票:8回答:3

我试图实现拉动刷新,但我有一个问题,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的父母,但没有任何成功,以及在LinearLayout内放置一个奇异的SwipeRefreshLayout。似乎唯一阻止滑动布局高度为0的东西是静态设置它但我希望它基于子视图的高度是动态的。

这里有什么我想念的吗?似乎SwipeRefreshLayout可能存在一个错误,因为用LinearLayout替换它也会按预期包装内容。

android android-layout android-appbarlayout swiperefreshlayout
3个回答
6
投票

问题是你的SwipeRefreshLayoutToolbarAppBarLayout内。你必须用另一个布局包装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>

1
投票

如果你的SwipeRefreshLayout高度包裹你的child_layout的内容,那么你的child_layoutheight应该设置为match_parent或者SwipeRefreshLayoutchild_layout都应该设置为match_parent,如Android Documentation所示

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

0
投票

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必须位于顶部以覆盖其他图层并且可以点击

© www.soinside.com 2019 - 2024. All rights reserved.