【问题标题】:CollapsingToolbarLayout move custom text to center when scrollingCollapsingToolbarLayout 滚动时将自定义文本移动到中心
【发布时间】:2018-08-30 02:22:59
【问题描述】:

我的布局应该是这样的:

有一些问题我需要解决:

  • 约束布局(下周,购物文本)应位于工具栏的底部。
  • 标题文本Next week在滚动完成后可以平滑移动到工具栏的中心
  • constraintLayout 底部有一个水平进度条
  • 滚动时需要固定水平进度条

这是我尝试过的:

还有我的布局:

 <androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:descendantFocusability="beforeDescendants"
    android:fitsSystemWindows="true"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />

            <!-- has a margin top with tool bar height-->
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginTop="?attr/actionBarSize"
                android:paddingLeft="@dimen/item_pad"
                android:paddingRight="@dimen/item_pad"
                app:layout_collapseMode="parallax">

                <TextView
                    android:id="@+id/tvTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/next_week"
                    android:textSize="@dimen/text_medium"
                    app:fontFamily="@font/averta_semi_bold"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/tvShoppingList"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/shopping_list"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_huge_x"
                    app:fontFamily="@font/averta_bold"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/tvTitle" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:tint="@color/main"
                    app:layout_constraintBottom_toBottomOf="@+id/tvShoppingList"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/tvShoppingList"
                    app:srcCompat="@drawable/ic_add" />


            </androidx.constraintlayout.widget.ConstraintLayout>

            <ProgressBar
                android:id="@+id/progressShopping"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom" />
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvShopping"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout> 

我该如何解决以上问题?

【问题讨论】:

    标签: android android-toolbar android-coordinatorlayout


    【解决方案1】:

    1) 在约束布局的顶部添加一个 48dp(工具栏高度)的内边距(或边距)

     android:paddingTop="48dp"
    

    2) 给AppBarLayout添加一个偏移量改变的监听器,根据可见的AppBar的verticalOffset翻译textView“Next Week”。

     appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            //Depending on the vertical offset of the AppBarLayout, change the translateY of the textView "Next Week" until it is zero by the time the AppBarLayout is fully collapsed.           
           }
       });
    

    3) & 4) 将 RecyclerView 容器布局更改为 LinearLayout 并将水平进度条视图放在 Recyler 视图上方,这样即使在应用栏完全折叠后它也会固定到顶部。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 2012-10-15
      • 2020-08-12
      • 2017-06-09
      • 2016-10-06
      相关资源
      最近更新 更多