1

これが私が達成したいことです。CollapsingToolbar の画像の下にカスタム レイアウトがあり、レイアウトにはいくつかのビュー アイテムが含まれます。1

必要な動作は、スクロール時に下部のレイアウトが徐々にフェードして消え、上部の画像のサイズが縮小し、ツールバーとして固定されることです。

画像だけを使用して CollapsingToolbar を既に作成しており、正常に動作しています。言ったように、私は一番下にカスタムレイアウトを追加することに固執していますが。

CollapsingToolbar をスキップして MotionLayout などを使用する必要がありますか??

<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:layout_height="match_parent"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
tools:ignore="RtlHardcoded">


<android.support.design.widget.AppBarLayout
    android:id="@+id/main.appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/main.collapsing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/sdv_add_iamge_header"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            app:overlayImage="@color/trans_black"
            android:tint="#11000000"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.5"
            app:actualImageScaleType="centerCrop"/>


        <FrameLayout
            android:id="@+id/main.framelayout.title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="230dp"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.2"
            app:layout_scrollFlags="scroll">

            <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            </RelativeLayout>
        </FrameLayout>


        <android.support.v7.widget.Toolbar
            android:id="@+id/main.toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin">
                <ImageView
                    android:id="@+id/btn_back"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/back"
                    style="@style/Widget.AppCompat.ActionButton"
                    android:layout_centerVertical="true"
                    android:layout_alignParentLeft="true" />

        </android.support.v7.widget.Toolbar>

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


        <com.jude.easyrecyclerview.EasyRecyclerView
            android:id="@+id/list"
            android:name="com.socialinfotech.feeedj.TimeLineActivities.ViewCompanyDetailsActivity"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="LinearLayoutManager"
            android:scrollbars="none"
            app:behavior_overlapTop="30dp"
            app:layout_progress="@layout/view_progrss"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context="com.socialinfotech.feeedj.TimeLineActivities.ViewCompanyDetailsActivity"
            tools:listitem="@layout/fragment_item" />

4

1 に答える 1

7

CollapsingToolbarLayout の代わりに、アニメーションを作成するための ConstraintLayout のサブクラスである強力なMotionLayoutを使用できます。既存のビュー階層を同等のフラットな MotionLayout に置き換えてもよろしい場合は、以下の詳細な回答をお読みください。


ここでは、必要に応じてヘッダー レイアウトの折りたたみアニメーションを 3 つの簡単な手順で作成する方法を紹介します。

コードを記述する前に、以下の gif 画像を参照して、作成しようとしているものをよりよく理解してください。

サンプル アニメーションを示す gif 画像

1. ConstraintLayout 依存関係を追加します。

プロジェクトで MotionLayout を使用するには、アプリの build.gradle ファイルに ConstraintLayout 2.0 依存関係を追加します。AndroidX を使用している場合は、次の依存関係を追加します。

dependencies {
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
}

2. MotionLayout ファイルを作成します。

MotionLayout は ConstraintLayout のサブクラスであるため、既存の ConstraintLayout を MotionLayout に変換できます。以下のようなレイアウトファイルを1つ作成します。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    motion:layoutDescription="@xml/motionscene"
    tools:showPaths="true">


    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toBottomOf="@id/toolbar">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:text="@string/large_text" />

    </androidx.core.widget.NestedScrollView>

    <ImageView
        android:id="@+id/headerBg"
        android:layout_width="match_parent"
        android:layout_height="@dimen/headerHeight"
        android:scaleType="centerCrop"
        android:src="@drawable/materialwallpaper"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/customHeader"
        android:layout_width="match_parent"
        android:layout_height="@dimen/headerHeight"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toTopOf="parent">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#66FF2345"
            android:text="custom header item"
            android:textColor="#ffffff" />

    </FrameLayout>


    <View
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/headerHeight"
        android:alpha="0"
        android:background="#345634"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toTopOf="parent" />


    <ImageView
        android:id="@+id/home"
        android:layout_width="wrap_content"
        android:layout_height="?attr/actionBarSize"
        android:padding="16dp"
        android:src="@drawable/abc_ic_ab_back_material"
        android:tint="?android:attr/textColorPrimaryInverse"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="0dp"
        android:layout_height="?attr/actionBarSize"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="8dp"
        android:gravity="center_vertical"
        android:text="Page Title"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        motion:layout_constraintBottom_toBottomOf="@id/customHeader"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.motion.widget.MotionLayout>

3. MotionScene を作成します。

前のステップでは、 app:layoutDescription 属性が MotionScene を参照しています。MotionScene は、対応するレイアウトのすべてのモーション記述を含む XML リソース ファイルです。レイアウト情報をモーション記述から分離しておくために、各 MotionLayout は個別の MotionScene を参照します。MotionScene の定義は、MotionLayout の同様の定義よりも優先されることに注意してください。

以下は、'enterAlways' 効果で必要な固定/固定ツールバーを作成するサンプル MotionScene ファイルです。

ファイルを res ディレクトリの下の xml フォルダーに配置します。

motionscene.xml

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@id/collapsed"
        motion:constraintSetStart="@id/expanded">

        <OnSwipe
            motion:dragDirection="dragUp"
            motion:moveWhenScrollAtTop="true"
            motion:touchAnchorId="@id/scrollView"
            motion:touchAnchorSide="top" />

    </Transition>

    <ConstraintSet android:id="@+id/expanded">
        <Constraint
            android:id="@id/headerBg"
            android:layout_height="@dimen/headerHeight"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent">


        </Constraint>
        <Constraint
            android:id="@id/customHeader"
            android:layout_width="match_parent"
            android:layout_height="@dimen/headerHeight"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent">
            <PropertySet android:alpha="1" />
        </Constraint>

        <Constraint
            android:id="@id/toolbar"
            android:layout_height="@dimen/headerHeight"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent">
            <PropertySet android:alpha="0" />
        </Constraint>

        <Constraint
            android:id="@+id/titleTextView"
            android:layout_width="0dp"
            android:layout_height="?attr/actionBarSize"
            android:layout_marginStart="16dp"
            android:gravity="center_vertical|left"
            motion:layout_constraintBottom_toBottomOf="@id/customHeader"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent">
            <CustomAttribute
                motion:attributeName="textSize"
                motion:customDimension="16sp" />

        </Constraint>


    </ConstraintSet>

    <ConstraintSet android:id="@+id/collapsed">
        <Constraint
            android:id="@id/headerBg"
            android:layout_height="?attr/actionBarSize"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent">

        </Constraint>
        <Constraint
            android:id="@id/customHeader"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent">
            <PropertySet android:alpha="0" />
        </Constraint>

        <Constraint
            android:id="@id/toolbar"
            android:layout_height="?attr/actionBarSize"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent">
            <PropertySet android:alpha="1" />
        </Constraint>
        <Constraint
            android:id="@id/titleTextView"
            android:layout_width="0dp"
            android:layout_height="?attr/actionBarSize"
            android:layout_marginStart="16dp"
            android:gravity="center_vertical|left"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toEndOf="@id/home"
            motion:layout_constraintTop_toTopOf="parent">
            <CustomAttribute
                motion:attributeName="textSize"
                motion:customDimension="10sp" />
        </Constraint>


    </ConstraintSet>

</MotionScene>

それで全部です。Java/kotlin コードを一切書かずに素晴らしいカスタム アニメーションを作成しました。MotionLayout は完全に宣言型です。つまり、どんなに複雑であっても、トランジションを XML で記述することができます。

于 2019-08-06T14:07:09.063 に答える