有没有一种方法可以在自定义视图中提取AppBarLayout及其CollapsingToolbar?

时间:2019-05-13 15:52:29

标签: android android-custom-view android-collapsingtoolbarlayout android-appbarlayout

创建工具栏后,在其中包含AppBarLayout和collapsingToolbarLayout;我想在customView中提取整个AppBarLayout以便重用,并通过属性进行管理。 但是当在自定义视图中提取布局时,我失去了使工具栏折叠的行为。 我想这是由于ViewGroup包装了我的自定义视图,导致片段丢失了行为。

如果任何人都可以说我,并以正确的方式处理它。

工具栏视图

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar_view_app_bar"
        android:fitsSystemWindows="true"
        android:layout_height="120dp"
        android:layout_width="match_parent">

    <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_view_layout"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:toolbarId="@+id/toolbar_view_toolbar"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_view_toolbar"
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin"/>

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

使用工具栏显示片段

<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_width="match_parent"
        android:layout_height="match_parent">

    <com.myproject.ui.view.HomeToolBar
            android:id="@+id/toolbar_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="8dp"
            app:title="Title Attribute"/>

    <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

        <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


        </android.support.constraint.ConstraintLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

HomeToolBar.kt

class HomeToolBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, attrs) {

    init {
        LayoutInflater.from(context).inflate(R.layout.home_toolbar_view, this, true)

        //set color on collapsable toolbar
        home_toolbar_view_layout.setCollapsedTitleTextColor(ContextCompat.getColor(this.context!!, R.color.home_color_white))
        home_toolbar_view_layout.setExpandedTitleColor(ContextCompat.getColor(this.context!!, R.color.home_color_white))

        attrs?.let{
            val typedArray = context.obtainStyledAttributes(it, R.styleable.HomeToolBar, 0 , 0)
            val title = typedArray.getString(R.styleable.HomeToolBar_title)
            home_toolbar_view_toolbar.title = title
            typedArray.recycle()
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您必须将CoordinatorLayout移出以包装AppBarLayout。