简体   繁体   English

以编程方式设置 app:layout_behavior

[英]Setting app:layout_behavior programmatically

I have a coordinator layout with a recyclerview which I would like to add programmatically.我有一个带有 recyclerview 的协调器布局,我想以编程方式添加它。 The reason why it's added programatically is because different fragments which inflate the coordinator layout, may use different types of recyclerviews.以编程方式添加它的原因是因为膨胀协调器布局的不同片段可能使用不同类型的回收器视图。

Typically for a recyclerview, in order to set this behaviour I would add it in the xml:通常对于回收站视图,为了设置此行为,我会将其添加到 xml 中:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

That works fine.这很好用。 However, I'm at a complete loss as to how to add this behavior when I create the recyclerviews programmatically and then add them to the framelayout:但是,当我以编程方式创建回收视图然后将它们添加到框架布局时,我完全不知道如何添加此行为:

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

Explanation解释

Behavior is a parameter of the CoordinatorLayout.LayoutParams . BehaviorCoordinatorLayout.LayoutParams一个参数。 You can set the behavior on an instance of CoordinatorLayout.LayoutParams with setBehavior method.您可以使用setBehavior方法在CoordinatorLayout.LayoutParams的实例上设置行为。

To get a proper Behavior object that represents the same thing as @string/appbar_scrolling_view_behavior you should create an instance of AppBarLayout.ScrollingViewBehavior .要获得与@string/appbar_scrolling_view_behavior相同的正确Behavior对象,您应该创建AppBarLayout.ScrollingViewBehavior的实例。


Example例子

( this is a cleaned up version of my previous edits to the original answer ) 这是我之前对原始答案的编辑的清理版本

First you will have to get an instance of a child View of your CoordinatorLayout .首先,您必须获得CoordinatorLayoutView的实例。 Let me get this clear: it is NOT the CoordinatorLayout itself.让我弄清楚这一点:它不是CoordinatorLayout本身。 childView is CoordinatorLayout's child. childViewCoordinatorLayout's孩子。

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

Assuming the childView is already attached to the CoordinatorLayout (so it already has LayoutParams ), you can do:假设childView已经附加到CoordinatorLayout (所以它已经有LayoutParams ),你可以这样做:

val params: CoordinatorLayout.LayoutParams = yourView.layoutParams as CoordinatorLayout.LayoutParams
params.behavior = AppBarLayout.ScrollingViewBehavior()
yourView.requestLayout()

To enable and disable layout_behavior programatically with kotlin use this code :要使用layout_behavior编程方式启用和禁用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
}

Note: replace swipeRefreshView with your view注意:用您的视图替换 swipeRefreshView

Accepted answer is correct but the provided code is not compilable.接受的答案是正确的,但提供的代码不可编译。 So here is a complete example所以这是一个完整的例子

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

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

2nd param is AttributeSet and it is fine to have it as null although it is not marked as Nullable in support lib.第二个参数是AttributeSet ,尽管它在支持库中没有被标记为Nullable ,但可以将其设置为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;app:layout_behavior&#39; 应该在哪里设置? - Where should 'app:layout_behavior' be set? 还有其他app:layout_behavior预定义值吗? - Are there other app:layout_behavior predefined values? 如何以编程方式打开和关闭layout_behavior行为? - How to toggle layout_behavior behavior on and off programmatically? 在工具栏应用程序中永远不会调用自定义滚动行为:layout_behavior - custom scroll behavior is never called in toolbar app:layout_behavior app:layout_behavior XML 如果使用 productFlavors 则崩溃 - app:layout_behavior XML crash if productFlavors used Android NestedScrollView 在 app:layout_behavior 后尺寸错误 - Android NestedScrollView has wrong size after app:layout_behavior 无法在片段标签上设置 `app:layout_behavior` - Cannot set `app:layout_behavior` on fragment tag 为子片段禁用“ app:layout_behavior =” @ string / appbar_scrolling_view_behavior”” - Disable 'app:layout_behavior=“@string/appbar_scrolling_view_behavior”' for child fragment 谁能解释一下“app:layout_behavior =”@ string / appbar_scrolling_view_behavior“? - Can anyone explain me about “app:layout_behavior=”@string/appbar_scrolling_view_behavior"? 我应该实现哪个库来使用 app:layout_behavior=“@string/appbar_scrolling_view_behavior” - which library should i implement to use app:layout_behavior=“@string/appbar_scrolling_view_behavior”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM