0

我正在使用BottomAppbar(在活动中)和Appbarlayout(在片段中)我想在滚动回收器视图时隐藏两者。现在只在 AppbarLayout 工具栏上方隐藏。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
    tools:context=".ui.mainscreen.MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="com.skapps.android.liboapp.ui.mainscreen.HomeFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navigation_graph"/>

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_app_bar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:layout_gravity="bottom"
        app:fabCradleMargin="8dp"
        app:fabCradleRoundedCornerRadius="32dp"
        android:backgroundTint="@color/colorPrimary"
        app:menu="@menu/bottom_app_bar_menu"
        app:fabAlignmentMode="end"
        app:hideOnScroll="true"
        app:navigationIcon="@drawable/ic_baseline_menu"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add_black_24dp"
        android:backgroundTint="@color/colorAccent"
        app:layout_anchor="@id/bottom_app_bar"
        app:maxImageSize="35dp"
        android:tint="@android:color/white"/>
    
</androidx.coordinatorlayout.widget.CoordinatorLayout>

片段主页.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/app_bar_shape"
        android:elevation="8dp"
        android:stateListAnimator="@null">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:layout_scrollFlags="scroll|enterAlways">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/pinBar"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <HorizontalScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginEnd="8dp"
                    android:scrollbars="none"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <com.google.android.material.chip.ChipGroup
                        android:id="@+id/chip_group_main"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="2dp"
                        app:chipSpacingHorizontal="8dp" />
                </HorizontalScrollView>

                <ImageButton
                    android:id="@+id/chips_select"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:background="@drawable/circle"
                    android:elevation="4dp"
                    android:contentDescription="@string/add_pin"
                    android:src="@drawable/ic_add_black_24dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

目前,只有上面的工具栏隐藏在滚动上,但如果我app:layout_scrollFlags="scroll|enterAlways"从工具栏中删除(在片段中),那么只有 bottomAppbar 隐藏在滚动上......我不知道。我试过很多,请帮忙。

4

1 回答 1

2

添加接口。

interface OnScrollListener {

   fun onScrolled() 

}

并在您的活动中实现此接口。

class Activity1 : AppCompatActivity(), OnScrollListener {

  override fun onScrolled() {
    hideBottomAppbar()
 }

}

在fragment的onAttach()方法中加入这个接口,在编写实现的时候调用监听器将appBarLayout隐藏在fragment中。

class fragment1: Fragment() {

var listener: OnScrollListener? = null

override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context == OnScrollListener)
            listener = OnScrollListener()
    }
}

//While scrolling, call this method
fun someMethod() {
  listener.onScrolled()
}

}
于 2020-07-27T11:30:17.537 回答