Есть ли способ извлечь AppBarLayout и его CollapsingToolbar в пользовательском представлении? - PullRequest
0 голосов
/ 13 мая 2019

После создания панели инструментов с AppBarLayout и collapsingToolbarLayout внутри;Я хочу извлечь весь AppBarLayout в customView, чтобы повторно использовать его и управлять им по атрибутам.Но когда макет извлекается в моем пользовательском представлении, я теряю поведение, сворачивающее панель инструментов.Я предполагаю, что это из-за того, что 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 голосов
/ 23 июля 2019

Вы должны переместить CoordinatorLayout, чтобы обернуть AppBarLayout.

...