[英]Setting app:layout_behavior programmatically
我有一個帶有 recyclerview 的協調器布局,我想以編程方式添加它。 以編程方式添加它的原因是因為膨脹協調器布局的不同片段可能使用不同類型的回收器視圖。
通常對於回收站視圖,為了設置此行為,我會將其添加到 xml 中:
app:layout_behavior="@string/appbar_scrolling_view_behavior"
這很好用。 但是,當我以編程方式創建回收視圖然后將它們添加到框架布局時,我完全不知道如何添加此行為:
<?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
是CoordinatorLayout.LayoutParams
一個參數。 您可以使用setBehavior
方法在CoordinatorLayout.LayoutParams
的實例上設置行為。
要獲得與@string/appbar_scrolling_view_behavior
相同的正確Behavior
對象,您應該創建AppBarLayout.ScrollingViewBehavior
的實例。
(這是我之前對原始答案的編輯的清理版本)
首先,您必須獲得CoordinatorLayout
的子View
的實例。 讓我弄清楚這一點:它不是CoordinatorLayout
本身。 childView
是CoordinatorLayout's
孩子。
//e.g. like this:
val childView: View = findViewById(R.id.child_view)
假設childView
已經附加到CoordinatorLayout
(所以它已經有LayoutParams
),你可以這樣做:
val params: CoordinatorLayout.LayoutParams = yourView.layoutParams as CoordinatorLayout.LayoutParams
params.behavior = AppBarLayout.ScrollingViewBehavior()
yourView.requestLayout()
要使用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
}
注意:用您的視圖替換 swipeRefreshView
接受的答案是正確的,但提供的代碼不可編譯。 所以這是一個完整的例子
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams)
view.getLayoutParams();
params.setBehavior(new AppBarLayout.ScrollingViewBehavior(view.getContext(), null));
第二個參數是AttributeSet
,盡管它在支持庫中沒有被標記為Nullable
,但可以將其設置為null
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.