滚动片段中的recyclerview时,CollapsingToolbarLayout不崩溃

时间:2019-07-02 16:49:31

标签: java android

我有一个NavigationActivity,其中的coordinatorLayout包含collapsingAppBarLayout以及bottomNavigationView和一个片段。我正在使用NavigationUI来管理所有这些东西。当我转到带有recyclerview的片段并滚动时,底部导航视图完美消失,但是顶部collapsingAppBarLayout却什么也没做。

我从4个小时的StackOverflow搜索中尝试了大约10亿件事情,但似乎都没有。我正在使用NavigationUI,DataBinding和Material Components,但它们似乎无法协同工作

这是我的NavigationActivity:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:id="@+id/godLayout"
    android:layout_height="match_parent">


    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="56dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            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"/>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        style="@style/Widget.MaterialComponents.BottomNavigationView"
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:itemIconTint="@drawable/bottom_navigation_colors"
        app:itemTextColor="@drawable/bottom_navigation_colors"
        app:itemRippleColor="@color/color_secondary_variant"
        app:menu="@menu/bottom_navigation_menu"
        app:hideOnScroll="true"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

以下是RecyclerView的片段:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="isLoading"
            type="boolean" />
    </data>


    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:refreshing="@{isLoading}">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/alarm_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:visibleGone="@{!isLoading}" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>



</layout>

以下是用于初始化导航控制器和内容的相关代码:

Set<Integer> topLevelDestinations = new HashSet<>();
        topLevelDestinations.add(R.id.dashboardFragment);
        topLevelDestinations.add(R.id.settingsFragment);
        topLevelDestinations.add(R.id.alarmsFragment);
        topLevelDestinations.add(R.id.vulnerabilitiesFragment);


        Toolbar toolbar = findViewById(R.id.toolbar);
        CollapsingToolbarLayout layout = findViewById(R.id.collapsing_toolbar_layout);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(topLevelDestinations)
                .build();

        layout.setTitle("dsljasonjda");
        NavigationUI.setupWithNavController(layout, toolbar, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(bottomNavigationView, navController);

我对此一无所知,在此先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

如果我没记错,则应使用NestedScrollView:示例 片段内部有三个片段,每个片段都有一个recyclerview和其他元素

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:fitsSystemWindows="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent">

            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <fragment
                    android:id="@+id/nav_host_fragment_container"
                    android:name="androidx.navigation.fragment.NavHostFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:defaultNavHost="true"
                    app:navGraph="@navigation/nav_home_store" />

            </androidx.appcompat.widget.LinearLayoutCompat>

        </androidx.core.widget.NestedScrollView>

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="280dp"
            app:liftOnScroll="false">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/ctlHomeStore"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="@color/green_00856A"
                app:expandedTitleGravity="top"
                app:expandedTitleMarginStart="@dimen/size_16dp"
                app:expandedTitleMarginTop="@dimen/size_40dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:scrimAnimationDuration="100">


                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/green_00856A">

                    <androidx.appcompat.widget.AppCompatTextView
                        android:id="@+id/txvDeliveryIn"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginStart="@dimen/size_16dp"
                        android:layout_marginBottom="@dimen/size_10dp"
                        android:background="?attr/selectableItemBackground"
                        android:drawablePadding="@dimen/size_5dp"
                        android:fontFamily="@font/source_sans_pro_regular"
                        android:text="Entrega en Bogotá"
                        android:textColor="@color/white"
                        android:textSize="@dimen/size_15sp"
                        android:typeface="normal"
                        app:drawableEndCompat="@drawable/flechaderblanca"
                        app:drawableStartCompat="@drawable/ic_location"
                        app:layout_collapseMode="parallax"
                        app:layout_constraintBottom_toTopOf="@+id/txvNextTime"
                        app:layout_constraintStart_toStartOf="parent" />

                    <androidx.appcompat.widget.AppCompatTextView
                        android:id="@+id/txvNextTime"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginStart="@dimen/size_16dp"
                        android:layout_marginBottom="140dp"
                        android:background="?attr/selectableItemBackground"
                        android:drawablePadding="@dimen/size_5dp"
                        android:fontFamily="@font/source_sans_pro_regular"
                        android:text="Próximo horario de entrega: Mañana"
                        android:textColor="@color/white"
                        android:textSize="@dimen/size_15sp"
                        android:typeface="normal"
                        app:drawableStartCompat="@drawable/ic_calendar"
                        app:layout_collapseMode="parallax"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent" />

                    <androidx.appcompat.widget.AppCompatImageView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:scaleType="fitXY"
                        app:layout_collapseMode="parallax"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:srcCompat="@drawable/bg_store" />

                    <androidx.appcompat.widget.AppCompatImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom|end"
                        android:layout_marginEnd="@dimen/size_16dp"
                        android:layout_marginBottom="60dp"
                        android:scaleType="fitXY"
                        app:layout_collapseMode="parallax"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:srcCompat="@drawable/ic_truck" />

                </androidx.constraintlayout.widget.ConstraintLayout>

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

                <RelativeLayout
                    android:id="@+id/rlContainer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tbHomeStore"
                    android:layout_gravity="bottom"
                    android:padding="@dimen/size_16dp">

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@+id/txvSearch"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/size_40dp"
                        android:background="@drawable/bg_corner_radius_home_store_white"
                        android:drawablePadding="@dimen/size_10dp"
                        android:hint="@string/title_search_home_store"
                        android:padding="@dimen/size_10dp"
                        android:textAlignment="textStart"
                        android:textColorHint="@android:color/darker_gray"
                        app:drawableStartCompat="@drawable/ic_search_green" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/rlContainer"
                    android:layout_gravity="end"
                    app:layout_collapseMode="pin">

                    <androidx.appcompat.widget.AppCompatImageView
                        android:id="@+id/cvCartIcon"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="end"
                        android:layout_marginTop="@dimen/size_15dp"
                        android:layout_marginEnd="@dimen/size_8dp"
                        app:layout_collapseMode="pin"
                        app:srcCompat="@drawable/ic_menu_cart" />
                </RelativeLayout>

            </com.google.android.material.appbar.CollapsingToolbarLayout>

        </com.google.android.material.appbar.AppBarLayout>

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bnvHomeStore"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            app:itemIconTint="@drawable/selector_bottom_nav"
            app:itemTextColor="@drawable/selector_bottom_nav"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>