解决RecyclerView遮住Toolber的问题

本文介绍了解决Android应用中RecyclerView遮挡Toolbar的问题。通过使用AppBarLayout及为RecyclerView指定特定的布局行为,可以避免RecyclerView遮挡Toolbar,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决RecyclerView遮住Toolber的问题

抓狂问题就像这张图片一样,来的太快,就像龙卷风.......抓狂

具体项目请访问我的Github

        我们来分析一下为什么RecyclerView会把Toolber遮住吧


1,首先看一下布局文件的代码


<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent" >

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width=""
            android:layout_height=""></android.support.design.widget.AppBarLayout>
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="16dp"
            android:src="@drawable/ic_done"
            android:elevation="8dp"/>
        <com.example.lab.android.nuc.materialtest.DragFloatActionButton
            android:id="@+id/fab_move"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|start"
            android:layout_margin="16sp"
            android:src="@drawable/ic_add"/>
    </android.support.design.widget.CoordinatorLayout>

    <!--<TextView-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent"-->
        <!--android:layout_gravity="start"-->
        <!--android:text="This is menu"-->
        <!--android:textSize="30sp"-->
        <!--android:background="#FFF"/>-->

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/nav_menu"
        app:headerLayout="@layout/nav_header" />

</android.support.v4.widget.DrawerLayout>
我们可以清楚的看到RecyclerView跟Toolbar都是放在中的,简化一下就是:

<android.support.design.widget.CoordinatorLayout

        <android.support.v7.widget.Toolbar

        <android.support.v7.widget.RecyclerView

</android.support.v4.widget.DrawerLayout>
想必大家都很清楚

CoordinatorLayout 就是加强版的FrameLayout

抓狂而FrameLayout中的控件在不进行明确定位的情况下,默认都会摆放到左上角抓狂


那么该如何解决呢?

  1. 传统情况下,使用偏移是唯一的解决办法惊恐惊恐惊恐
  2. 真叫人脑阔疼惊恐惊恐惊恐
  3. 但是:

我们使用的可不是普通的FrameLayout,而是CoordinatorLayout,因此自然会有一些更加巧妙地方法了微笑微笑微笑


即:Design support库中提供的另外的一个工具——AppBarLayout


        那么我们怎样用AppBarLayout才能解决前面的覆盖问题呢?

其实只需要两步就可以了:

1.将ToolBar嵌套到AppBarLayout中
2.给RecyclerView指定一个布局行为

布局代码更改为:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent" >

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
        </android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" //谨记这里不是match_view,我自己就错在这里了
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="16dp"
            android:src="@drawable/ic_done"
            android:elevation="8dp"/>
        <com.example.lab.android.nuc.materialtest.DragFloatActionButton
            android:id="@+id/fab_move"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|start"
            android:layout_margin="16sp"
            android:src="@drawable/ic_add"/>
    </android.support.design.widget.CoordinatorLayout>

    <!--<TextView-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent"-->
        <!--android:layout_gravity="start"-->
        <!--android:text="This is menu"-->
        <!--android:textSize="30sp"-->
        <!--android:background="#FFF"/>-->

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/nav_menu"
        app:headerLayout="@layout/nav_header" />

</android.support.v4.widget.DrawerLayout>

简化一下就是:

<android.support.design.widget.CoordinatorLayout


           
<android.support.design.widget.AppBarLayout
         <android.support.v7.widget.Toolbar
 </android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
 添加一行代码:app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

效果如图:

在Toolbar里面添加一行代码就会显得更人性化

.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll | enterAlways | snap"/>

待我一一解释:

scroll :当RecyclerView向上滚动的时候,ToolBar会跟着一起向上滚动并实现隐藏

enterAlways :当RecyclerView向下滚动的时候,Toolbar会跟着一起向下滚动并重新显示
snap :当ToolBar还没有隐藏或者显示的时候,会根据当前滚动的距离,自动选择时显示还是隐藏

演示一下:


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值