【问题标题】:Coordinator layout - collapsing content interaction with recycler view协调器布局 - 折叠与回收器视图的内容交互
【发布时间】:2019-12-24 21:49:47
【问题描述】:

schema

如图所示,我希望我的 2 个视图(回收站视图和折叠内容)只响应中间的夹子栏(它可能是 50dp 高度约束布局 - 例如) 移动这个栏应该折叠上部 - 并使 recyclerview 改变它的高度。该栏不应低于上部内容。回收站视图应该可以一直独立滚动。

如何只让受限区域响应触摸事件? 如何使recyclerview高度取决于握把位置?

现在在我的例子中,折叠部分在我开始滚动 recyclerview 之后就被隐藏了。

如果没有足够的信息,我会添加任何需要的。

代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/activityToDo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.ToDoActivity">

    <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbar"
                app:layout_scrollFlags="scroll|enterAlways"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


            <androidx.constraintlayout.widget.ConstraintLayout
                    app:layout_collapseMode="parallax"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                <EditText
                        android:hint="Note title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/noteTitle"
                        app:layout_constraintStart_toStartOf="parent"
                        android:layout_marginStart="10dp"
                        app:layout_constraintEnd_toStartOf="@+id/guideline"
                        android:layout_marginEnd="10dp"
                        android:layout_marginTop="15dp"
                        android:textSize="15sp"
                        android:inputType="text" app:layout_constraintTop_toTopOf="parent"/>
                <EditText
                        android:hint="Note priority"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/notePriority"
                        app:layout_constraintEnd_toEndOf="parent"
                        android:layout_marginEnd="10dp"
                        app:layout_constraintStart_toStartOf="@+id/guideline"
                        android:layout_marginStart="10dp"
                        android:layout_marginTop="15dp"
                        android:textSize="15sp"
                        android:inputType="number" app:layout_constraintTop_toTopOf="parent"/>

            <androidx.constraintlayout.widget.Guideline
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintGuide_begin="197dp"
                    android:id="@+id/guideline"/>

                <EditText
                        android:hint="Note description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/noteDescription"
                        app:layout_constraintEnd_toEndOf="parent"
                        android:layout_marginEnd="10dp"
                        app:layout_constraintStart_toStartOf="parent"
                        android:layout_marginStart="10dp"
                        app:layout_constraintTop_toBottomOf="@+id/notePriority"
                        android:layout_marginTop="20dp"
                        android:textSize="15sp"/>
                <Button
                        android:text="Save"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/saveButton"
                        app:layout_constraintStart_toStartOf="parent"
                        android:layout_marginStart="10dp"
                        app:layout_constraintEnd_toStartOf="@+id/guideline" android:layout_marginEnd="10dp"
                        android:layout_marginBottom="20dp" app:layout_constraintBottom_toBottomOf="parent"
                        android:layout_marginTop="10dp" app:layout_constraintTop_toBottomOf="@+id/noteDescription"/>
                <Button
                        android:text="Clear"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/clearButton"
                        app:layout_constraintEnd_toEndOf="parent"
                        android:layout_marginEnd="10dp" app:layout_constraintStart_toStartOf="@+id/guideline"
                        android:layout_marginStart="10dp" android:layout_marginBottom="20dp"
                        app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="10dp"
                        app:layout_constraintTop_toBottomOf="@+id/noteDescription"/>

            </androidx.constraintlayout.widget.ConstraintLayout>



        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <TextView
                android:text="All notes"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

    </com.google.android.material.appbar.AppBarLayout>


    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

    <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/allNotesRecyclerView"
            android:layout_marginStart="8dp"
            app:layout_constraintTop_toTopOf="@id/collapsingToolbar"
            android:layout_marginTop="16dp"
            android:textSize="15sp"/>

    </LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

GitHub Repo

【问题讨论】:

    标签: android android-layout android-coordinatorlayout android-collapsingtoolbarlayout android-appbarlayout


    【解决方案1】:

    使用最新版本的 Android Studio (4.0 Canary 9),您可以使用新的 MotionLayout layout type 在不同的 ConstraintLayout 之间进行转换。最简单的解决方案(如下)不一定让您将 RecyclerView 高度更改为您想要的任何高度,但是当用户向上或向下滑动时,您可以在两种不同的布局之间平滑过渡。

    简而言之,您将执行以下操作:

    1. 在您的 build.gradle (Module: app) 文件中,添加以下行:

      implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'

    2. 创建一个新的 ConstraintLayout 布局文件,用于确定所有视图在开始时应如何排列

    3. 使用布局编辑器中的右键菜单将该 ConstraintLayout 转换为 MotionLayout
    4. 在 MotionLayout 编辑器中,为“开始”和“结束”布局中的每个视图添加约束

    5. 在添加到 res 文件夹中 XML 文件夹的新 MotionScene XML 文件中,编辑标签,使其如下所示:

    <Transition
          motion:constraintSetEnd="@+id/end"
          motion:constraintSetStart="@id/start"
           >
           <OnSwipe               
                    app:touchAnchorSide="top"
                    motion:touchAnchorId="@id/YOUR_GRIP_VIEW"
                    motion:dragDirection="dragUp" />
               <KeyFrameSet>
               </KeyFrameSet>
     </Transition>
    

    * 在MotionLayout Codelab 中,它说要使用 app:tou​​chAnchorID 而不是 motion:touchAnchorId 但这对我不起作用 *

    现在,当您向上或向下滑动夹点视图时,MotionLayout 可以在状态之间转换。

    【讨论】:

      【解决方案2】:

      首先从recyclerview 的父级删除您的LinearLayout试试这个:

       <androidx.coordinatorlayout.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">
      
          <com.google.android.material.appbar.AppBarLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
              <com.google.android.material.appbar.CollapsingToolbarLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  app:layout_scrollFlags="scroll|exitUntilCollapsed">
      
                  <!-- HEADER -->
                  <RelativeLayout
                      ...
                      app:layout_collapseMode="parallax">
                      .....
                  </RelativeLayout>
      
                  <androidx.appcompat.widget.Toolbar
                      android:layout_width="match_parent"
                      android:layout_height="?attr/actionBarSize"
                      app:layout_collapseMode="pin" />
      
              </com.google.android.material.appbar.CollapsingToolbarLayout>
      
             <!-- IF YOU WANT TO KEEP "Choose Item" always on top of the RecyclerView, put this TextView here
              <TextView
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:layout_gravity="bottom"
                   android:text="choose item" />
             -->
          </com.google.android.material.appbar.AppBarLayout>
      
          <androidx.recyclerview.widget.RecyclerView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior" />
      
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
      

      同样的问题答案:Link

      【讨论】:

        【解决方案3】:

        在我的情况下是这样的: 在你的 recyclerview 上添加 app:layout_behavior="@string/appbar_scrolling_view_behavior" 并查看我的 CollapsingToolbarLayout 和我的工具栏

        <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/coordinatorLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:context=".myactivity.DetailWisataActivity">
        
            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/app_bar_detail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        
                <com.google.android.material.appbar.CollapsingToolbarLayout
                    android:id="@+id/collapse_toolbar_detail"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:fitsSystemWindows="true"
                    app:contentScrim="?colorPrimary"
                    app:expandedTitleGravity="center_horizontal|bottom"
                    app:layout_scrollFlags="scroll|snap|exitUntilCollapsed">
        
                    <ImageView
                        android:id="@+id/img_bg_detail"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:contentDescription="@string/app_name"
                        android:scaleType="centerCrop"
                        android:src="@drawable/bg_beach"
                        app:layout_collapseMode="pin"
                        tools:targetApi="m" />
        
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@color/colorTransparantBlackLow" />
        
                    <ProgressBar
                        android:id="@+id/progress_detail"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center" />
        
                    <androidx.appcompat.widget.Toolbar
                        android:id="@+id/toolbar_detail"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/actionBarSizeLow"
                        android:theme="@style/ThemeOverlay.AppCompat.Light"
                        app:layout_collapseMode="pin"
                        app:title="@string/info_wisata" />
                </com.google.android.material.appbar.CollapsingToolbarLayout>
            </com.google.android.material.appbar.AppBarLayout>
        
           ............Your recyclerview here.......
        
            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
        

        并删除您的线性布局。

        【讨论】:

        • 它的结果和我的完全一样。不能解决我的问题
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-11
        • 1970-01-01
        • 1970-01-01
        • 2016-10-06
        • 1970-01-01
        • 2017-05-05
        • 1970-01-01
        相关资源
        最近更新 更多