以编程方式设置 app:layout_behavior。

40
我有一个协调布局,其中包含一个RecyclerView,我想以编程方式添加它。之所以以编程方式添加它,是因为充气协调布局的不同片段可能使用不同类型的RecyclerView。
通常,为了设置RecyclerView的行为,我会在xml中添加它:
app:layout_behavior="@string/appbar_scrolling_view_behavior"

这个方案可以正常工作。但是,当我以编程方式创建RecyclerView并将它们添加到FrameLayout时,我完全不知道如何添加这种行为:

那么就是说您需要动态地在代码中创建RecyclerView并将其添加到FrameLayout中,但是您不知道如何为这些动态创建的RecyclerView添加某些行为。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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:layout_width="match_parent"
    android:id="@+id/coordLayout"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout android:id="@+id/app_bar"
        android:fitsSystemWindows="true" android:layout_height="@dimen/app_bar_height"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout"
            android:fitsSystemWindows="true" android:layout_width="match_parent"
            android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>
    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </FrameLayout>

</android.support.design.widget.CoordinatorLayout>
3个回答

106

说明

Behavior(行为)CoordinatorLayout.LayoutParams(协调布局参数)的一个参数。您可以使用setBehavior方法在CoordinatorLayout.LayoutParams的实例上设置行为。

要获取一个合适的Behavior(行为)对象,该对象表示与@string/appbar_scrolling_view_behavior相同的内容,您应该创建一个AppBarLayout.ScrollingViewBehavior的实例。


示例

(这是对原答案进行整理后的版本)

首先,您需要获取您的CoordinatorLayoutchild(子项)View的一个实例。让我清楚地说明一下:这不是CoordinatorLayout本身。 childViewCoordinatorLayout的子项。

//e.g. like this:
val childView: View = findViewById(R.id.child_view)

假设 childView 已经附加到了 CoordinatorLayout 上(因此已经具有 LayoutParams),您可以执行以下操作:

假定 childView 已附加到 CoordinatorLayout 上(因此具有 LayoutParams),则可以执行以下操作:

(注意:以上两句话意思相同,仅为中英文两种语言表达习惯的差异,选择其中任意一句即可)
val params: CoordinatorLayout.LayoutParams = yourView.layoutParams as CoordinatorLayout.LayoutParams
params.behavior = AppBarLayout.ScrollingViewBehavior()
yourView.requestLayout()

1
你有代码实现的例子吗? - AndroidP
1
当加载我的片段时,setBehavior 导致 XML 解析错误,它无法正常工作。对于具有自己类和 XML 的片段,我们该如何处理?我注意到文档中有一个未记录的 public AppBarLayout.ScrollingViewBehavior(Context context, AttributeSet attrs) - roberto tomás
同样的行为也可以应用于LinearLayouts,但我还没有能够以编程方式添加它。你知道怎么做吗?我在这里提出了一个问题:http://stackoverflow.com/questions/38530196/how-to-add-and-remove-layout-behavior-programmatically-to-linearlayout - Aspiring Dev
@Mark13426,我没有提到RecyclerView,但是我猜你的问题是在问ListView是否也可以与CollapsingToolbarLayout一起使用。答案是否定的。CollapsingToolbarLayout希望你使用实现NestedScrollingChild接口的View。你可以自己去实现它(对于ListView),但相比重新使用RecyclerView来重新实现屏幕,这似乎是一件很麻烦的事情。 - Bartek Lipinski
我想为我动态添加到协调布局中的视图设置行为。因此,我正在调用mCoordinatorLayout.addView(customView),并且我想设置自定义视图的行为。但是,自定义视图似乎认为它的布局参数不是CoordinatorLayout.Params。 - fobbymaster
显示剩余7条评论

9

要使用 Kotlin 程序化地启用和禁用 layout_behavior,请使用以下代码:

fun enableLayoutBehaviour() {
    val param: CoordinatorLayout.LayoutParams = swipeRefreshView.layoutParams as CoordinatorLayout.LayoutParams
    param.behavior = AppBarLayout.ScrollingViewBehavior()
}

fun disableLayoutBehaviour() {
    val param: CoordinatorLayout.LayoutParams = swipeRefreshView.layoutParams as CoordinatorLayout.LayoutParams
    param.behavior = null
}

注意: 将swipeRefreshView替换为您的视图。

一旦我们禁用布局行为,隐藏的底部导航视图会自动重新出现吗? - K Pradeep Kumar Reddy

5

被接受的答案是正确的,但提供的代码无法编译。因此,在这里提供一个完整的示例:

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) 
view.getLayoutParams();

params.setBehavior(new AppBarLayout.ScrollingViewBehavior(view.getContext(), null));

第二个参数是 AttributeSet ,虽然在支持库中它没有被标记为 Nullable,但将其设为 null 是可以的。


2
为什么我的答案中的代码无法编译?对我来说,它似乎可以正常编译? - Bartek Lipinski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接