问题
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>
回答1:
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.
Edit 1:
I don't really have an example, but I can maybe write you one. Assuming you have a View (e.g. yourView) that is already attached to a CoordinatorLayout (so it already has LayoutParams).
CoordinatorLayout.LayoutParams params =
(CoordinatorLayout.LayoutParams) yourView.getLayoutParams();
params.setBehavior(new AppBarLayout.ScrollingViewBehavior());
yourView.requestLayout();
You might need to tweak that a little (I haven't tested that, but it should work).
Edit 2:
Just a small disclaimer because not everyone seem to understand the example perfectly.
yourView mentioned in the example above is NOT the CoordinatorLayout itself. yourView is CoordinatorLayout's child View.
回答2:
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.
回答3:
To enable and disable layout_behavior programatically with kotlin use this code :
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
来源:https://stackoverflow.com/questions/33707337/setting-applayout-behavior-programmatically