我正在尝试使用带有CollapsingToolbarLayout
和自定义appBar标题的CoordinatorLayout来制作android应用。
所以我想使用自定义CoordinatorLayout.Behavior
在片段内组织我的LinearLayout,以更改折叠工具栏上的布局。
我的activity.xml
<?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"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fillViewport="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/categoryAppBar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/categoryCollapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/thai_1024"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/categoryToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/category_appbar_container"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:elevation="@dimen/level_8">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
活动分类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
mtoolbar = (Toolbar) findViewById(R.id.categoryToolbar);
setSupportActionBar(mtoolbar);
String categoryId = intent.getExtras().getString("categoryId");
CategoryViewModel categoryViewModel = ViewModelProviders.of(this, viewModelFactory).get(CategoryViewModel.class);
categoryViewModel.init(categoryId);
observeViewModel(categoryViewModel);
}
private void startAppbarCategoryAppbarFragment() {
categoryAppbarFragment = new CategoryAppbarFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.category_appbar_container, categoryAppbarFragment)
.commit();
}
private void observeViewModel(final CategoryViewModel categoryViewModel) {
// Observe Category Data
categoryViewModel.getCategory().observe(this, new Observer<Category>() {
@Override
public void onChanged(@Nullable Category category) {
// Update UI
if (category != null) {
startAppbarCategoryAppbarFragment();
} else {
Log.e(TAG, "Category Data is Null");
}
}
});
}
我的fragment_appbar.xml(我用于将布局充气到CategoryAppbarFragment中)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:gravity="bottom"
android:orientation="horizontal"
app:layout_behavior="@string/collapsing_category_appbar_behavior">
<ImageView
android:id="@+id/appBarCategoryImage"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginBottom="12dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:background="@drawable/rounded_box"
android:contentDescription="Category Image"/>
<RelativeLayout
android:id="@+id/appBarTextContainer"
android:layout_width="wrap_content"
android:layout_height="@dimen/app_bar_height"
android:gravity="bottom"
android:orientation="vertical">
<TextView
android:id="@+id/appBarCategoryTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Test CAt title"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/appBarCategorySubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="12dp"
android:text="fingerprint_icon_content_description" />
</RelativeLayout>
</LinearLayout>
问题是,当我像这样使用collapsing_category_appbar_behavior
时,它将无法正常工作。
但是,如果我将CategoryAppbarFragment
取出并将<LinearLayout>
直接放在activity.xml
内,则效果很好。
那么如何使这种行为在片段<FrameLayout>
中起作用?
谢谢!