【问题标题】:Android coordinatorlayout add layout below RecyclerViewAndroid coordinatorlayout 在 RecyclerView 下面添加布局
【发布时间】:2019-07-25 08:19:53
【问题描述】:

为了能够支持底部工作表行为 我必须在RecyclerView 下方添加我的entry_field 布局。 layout_anchor 将无法正常工作。 在这种情况下,entry_field 将阻止RecyclerView

我希望 RecyclerView 填充空格的重置并使用 entry_field 调整大小

如果entry_field 的高度发生变化,RecyclerView 应该始终适合重置的空间,而不是被entry_field 阻挡

entry_field 必须是 CoordinatorLayout 的直接子代

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/background_gray_lightest">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/messaging_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:overScrollMode="ifContentScrolls"
    android:paddingTop="8dp"
    android:paddingBottom="10dp"
    android:scrollbars="vertical"
    android:layout_gravity="top"
    tools:listitem="@layout/item_message" />

<RelativeLayout
    android:id="@+id/entry_field"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:layout_anchor="@id/messaging_recycler_view"
    app:layout_anchorGravity="bottom">

    <com.abc.newmessage.NewMessageLayout
        android:id="@+id/include_message_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </com.abc.newmessage.NewMessageLayout>
</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-recyclerview android-coordinatorlayout


    【解决方案1】:

    要实现这一点,我认为您必须创建自己的 CoordinatorLayout.Behavior

    类似的东西(在我的脑海中写下这个,所以你可能需要更正那个):

    class FillBehavior : CoordinatorLayout.Behavior<View> {
    
        @Suppress("unused")
        constructor() : super()
    
        @Suppress("unused")
        constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    
        private var lastSetHeight = -1
    
        override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
            return dependency.id == R.id.entry_field
        }
    
        override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
            val leftVerticalSpace = parent.height - dependency.height
            if (lastSetHeight != leftVerticalSpace) {
                lastSetHeight = leftVerticalSpace;
                child.layoutParams.height = lastSetHeight
                child.requestLayout()
                return true
            } else {
                //nothing to do, available space hasn't changed
                return false
            }
        }
    }
    

    然后在你的布局中:

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/messaging_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:overScrollMode="ifContentScrolls"
        android:paddingTop="8dp"
        android:paddingBottom="10dp"
        android:scrollbars="vertical"
        android:layout_gravity="top"
        tools:listitem="@layout/item_message"         
        app:layout_behavior="your.package.FillBehavior"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      相关资源
      最近更新 更多