【问题标题】:Toolbar is scrollable eventhough recyclerview is empty即使 recyclerview 为空,工具栏也是滚动条
【发布时间】:2022-01-22 09:44:33
【问题描述】:

我有以下布局:

<?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"
    android:fitsSystemWindows="true"
    android:focusableInTouchMode="true"

    >

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/abLayoutDriver"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/BrightYellowCrayola">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/tbDriver"
            app:navigationIcon="@drawable/baseline_menu_24"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            >

            <androidx.appcompat.widget.SearchView
                android:id="@+id/svDriver"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:defaultQueryHint="@string/search_drivers"
                app:iconifiedByDefault="false"
                app:searchIcon="@null"
                app:queryBackground="@android:color/transparent"
                app:submitBackground="@android:color/transparent"
                android:imeOptions="flagNoExtractUi"

                />

        </androidx.appcompat.widget.Toolbar>



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


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvListDriver"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="@dimen/zero_margin_when_normal"
            android:paddingEnd="@dimen/zero_margin_when_normal"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabAddDeleteDriver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:contentDescription="@string/add_driver"
        android:minHeight="48dp"
        android:src="@drawable/baseline_person_add_24"
        app:backgroundTint="@color/BrightYellowCrayola" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

为了在滚动回收站视图时隐藏Toolbar,我使用app:layout_scrollFlags="scroll|enterAlways。 但是,这会导致关注issue。现在工具栏本身可以上下滚动,即使 recyclerview 是空的。如果我删除属性app:layout_scrollFlags="scroll|enterAlways 我就没有这个问题了,但是当我向下滚动recyclerview时工具栏将不再向下滚动..

知道如何解决这个问题,让滚动只在 recyclerview 滚动时起作用?

【问题讨论】:

    标签: android android-recyclerview scroll scrollview android-collapsingtoolbarlayout


    【解决方案1】:
      val toolbar: Toolbar =
            findViewById(R.id.toolbar) // or however you need to do it for your code
    
        if (listsize == 0){
            toolbar.layoutParams.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL
        }else{
            toolbar.layoutParams.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
        }
    

    【讨论】:

      猜你喜欢
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 1970-01-01
      • 2016-05-18
      相关资源
      最近更新 更多