プログラムで追加したいrecyclerviewを備えたコーディネーターレイアウトがあります。プログラムで追加される理由は、コーディネーターレイアウトを膨らませるさまざまなフラグメントが、さまざまなタイプのリサイクラービューを使用する可能性があるためです。
通常、recyclerviewの場合、この動作を設定するために、xmlに追加します。
app:layout_behavior="@string/appbar_scrolling_view_behavior"
それはうまくいきます。ただし、プログラムでrecyclerviewsを作成し、それらをフレームレイアウトに追加するときに、この動作を追加する方法については完全に途方に暮れています。
<?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
。CoordinatorLayout.LayoutParams
withsetBehavior
メソッドのインスタンスで動作を設定できます。
のインスタンスを作成する必要がBehavior
あるのと同じことを表す適切なオブジェクトを取得するに@string/appbar_scrolling_view_behavior
はAppBarLayout.ScrollingViewBehavior
。
編集1:
例はありませんが、書くことはできます。にすでに接続されているView
(例yourView
)があると仮定しますCoordinatorLayout
(したがって、すでに接続されていますLayoutParams
)。
CoordinatorLayout.LayoutParams params =
(CoordinatorLayout.LayoutParams) yourView.getLayoutParams();
params.setBehavior(new AppBarLayout.ScrollingViewBehavior());
yourView.requestLayout();
あなたはそれを少し微調整する必要があるかもしれません(私はそれをテストしていませんが、それはうまくいくはずです)。
編集2:
誰もが例を完全に理解しているわけではないように見えるので、ほんの小さな免責事項です。
yourView
上記の例で述べたのはそれ自体ではありませんCoordinatorLayout
。yourView
あるCoordinatorLayout's
子View
。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加