首页 > 解决方案 > 有没有办法将recyclerview和linearlayout放入swiperefreshlayout?

问题描述

如果我只RecyclerView放入SwipeRefreshLayout上面的按钮RecyclerView。如果我全力以赴,SwipeRefresh我的按钮就会消失

<android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
            android:id="@+id/students_list"
            android:layout_width="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/buttonsLayout">
    </android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>

<LinearLayout
        android:id="@+id/buttonsLayout"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:orientation="horizontal">

    <android.support.v7.widget.AppCompatButton
            android:id="@+id/selectAllButton"
            android:text="Выбрать всех"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</LinearLayout>

有没有什么办法解决这一问题?

标签: androidandroid-layoutkotlinandroid-recyclerview

解决方案


SwipeRefreshLayout 只接受一个孩子,所以也许你可以在 SwipeRefreshLayout 内有一个 ConstraintLayout,在 ConstraintLayout 内有一个带有按钮的 RecyclerView。

PS:我建议使用 ConstraintLayout 来避免嵌套布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.constraint.ConstraintLayout>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/students_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/selectAllButton"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/selectAllButton"
            android:text="Выбрать всех"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />
    </android.support.constraint.ConstraintLayout>
</android.support.v4.widget.SwipeRefreshLayout>

推荐阅读