Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting app:layout_behavior programmatically

I have a coordinator layout with a recyclerview which I would like to add programmatically. 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:

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> 
like image 769
AndroidP Avatar asked Nov 14 '15 10:11

AndroidP


People also ask

How do you set layout behavior programmatically?

You can set the behavior on an instance of CoordinatorLayout. LayoutParams with setBehavior method. 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 .

How do I use CoordinatorLayout?

By specifying Behaviors for child views of a CoordinatorLayout you can provide many different interactions within a single parent and those views can also interact with one another. View classes can specify a default behavior when used as a child of a CoordinatorLayout by implementing the AttachedBehavior interface.


1 Answers

Explanation

Behavior is a parameter of the CoordinatorLayout.LayoutParams. You can set the behavior on an instance of CoordinatorLayout.LayoutParams with setBehavior method.

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.


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. Let me get this clear: it is NOT the CoordinatorLayout itself. childView is CoordinatorLayout's child.

//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:

val params: CoordinatorLayout.LayoutParams = yourView.layoutParams as CoordinatorLayout.LayoutParams params.behavior = AppBarLayout.ScrollingViewBehavior() yourView.requestLayout() 
like image 58
Bartek Lipinski Avatar answered Sep 22 '22 09:09

Bartek Lipinski