通常,为了设置RecyclerView的行为,我会在xml中添加它:
app:layout_behavior="@string/appbar_scrolling_view_behavior"
这个方案可以正常工作。但是,当我以编程方式创建RecyclerView并将它们添加到FrameLayout时,我完全不知道如何添加这种行为:
那么就是说您需要动态地在代码中创建RecyclerView并将其添加到FrameLayout中,但是您不知道如何为这些动态创建的RecyclerView添加某些行为。
<?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>
setBehavior
导致 XML 解析错误,它无法正常工作。对于具有自己类和 XML 的片段,我们该如何处理?我注意到文档中有一个未记录的public AppBarLayout.ScrollingViewBehavior(Context context, AttributeSet attrs)
。 - roberto tomás