[英]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>
Behavior
is a parameter of the CoordinatorLayout.LayoutParams
. Behavior
是CoordinatorLayout.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
的实例。
( 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
.首先,您必须获得
CoordinatorLayout
的子View
的实例。 Let me get this clear: it is NOT the CoordinatorLayout
itself.让我弄清楚这一点:它不是
CoordinatorLayout
本身。 childView
is CoordinatorLayout's
child. childView
是CoordinatorLayout'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.