安卓开发中上滑触顶悬停

要实现上滑触顶悬停要用的控件有:

CoordinatorLayout、AppBarLayout  、CollapsingToolbarLayout、NestedScrollView

  1. CoordinatorLayout 作为根布局或包裹 AppBarLayout 的布局。
  2. AppBarLayout 放置在 CoordinatorLayout 中,用于构建顶部导航栏。
  3. CollapsingToolbarLayout 包含在 AppBarLayout 中,用于实现标题和工具栏的折叠效果(即需要隐藏的界面)。
  4. NestedScrollView 或其他滚动视图作为 CoordinatorLayout 的子视图,用于展示可滚动的内容。

1、结构图

 

        因为想试试引入布局能不能正常使用,所以编辑了一个hide.xml文件作为隐藏界面的布局,然后在activity_main.xml文件中引用:

2、hide.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="360dp"
    android:orientation="vertical">
    <androidx.appcompat.widget.SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="true"/>


    <LinearLayout
        android:background="#3336"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:text="试卷翻新"
            android:layout_gravity="center"
            android:layout_marginStart="20dp"/>
        <TextView
            android:layout_width="0dp"
            android:gravity="center_horizontal"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="错题打印"
            android:layout_gravity="center"
            android:layout_marginStart="20dp"/>

        <TextView
            android:gravity="center_horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="精品试卷"
            android:layout_gravity="center"
            android:layout_marginStart="20dp"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:background="#9987"
        android:layout_width="match_parent"
        android:layout_height="90dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:text="Vip专区"
            android:layout_gravity="center"
            android:layout_marginStart="20dp"/>
        <TextView
            android:layout_width="0dp"
            android:gravity="center_horizontal"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="课后刷题"
            android:layout_gravity="center"
            android:layout_marginStart="20dp"/>

        <TextView
            android:gravity="center_horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="AI金准学"
            android:layout_gravity="center"
            android:layout_marginStart="20dp"/>
        <TextView
            android:layout_width="0dp"
            android:gravity="center_horizontal"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="易错题"
            android:layout_gravity="center"
            android:layout_marginStart="20dp"/>

        <TextView
            android:gravity="center_horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="作业批改"
            android:layout_gravity="center"
            android:layout_marginStart="20dp"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="160dp">
        <LinearLayout
            android:layout_marginStart="5dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="5dp"
            android:background="#3432"
            android:layout_width="0dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:gravity="center">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="期末复习专区"
                android:layout_gravity="center"
                android:textSize="30dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_marginStart="5dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="5dp"
            android:background="#3432"
            android:layout_width="0dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <LinearLayout

                android:layout_marginStart="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_marginEnd="5dp"
                android:background="#456119"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="期末复习宝典"
                    android:layout_gravity="center"
                    android:textColor="@color/white"/>
            </LinearLayout>

            <LinearLayout
                android:layout_marginStart="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_marginEnd="5dp"
                android:background="#987789"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="期末复习宝典"
                    android:layout_gravity="center"
                    android:textColor="@color/white"/>

            </LinearLayout>
        </LinearLayout>

    </LinearLayout>


</LinearLayout>

 

3、activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical">
    <LinearLayout
        android:background="#998766"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_marginTop="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="头部"
            android:textSize="50sp" />


    </LinearLayout>

    <!-- 第一部分:CoordinatorLayout -->
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="#ffffff">
        <!-- 第二部分:缩放控件-->
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="0dp">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll">

                <LinearLayout
                    android:background="#9999"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">


                    <TextView
                        android:layout_marginTop="30dp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:text="可隐藏界面"
                        android:textSize="50sp" />
                    <include
                        android:id="@+id/hide"
                        layout="@layout/hide"/>
                </LinearLayout>
            </com.google.android.material.appbar.CollapsingToolbarLayout>



                <LinearLayout
                    android:background="#3336"
                    android:layout_width="match_parent"
                    android:layout_height="70dp"
                    android:orientation="horizontal">
                    <TextView
                        android:id="@+id/chinese"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="24sp"
                        android:text="@string/chinese"
                        android:layout_gravity="center_vertical"
                        android:layout_marginStart="70dp"/>
                    <TextView
                        android:id="@+id/math"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="24sp"
                        android:text="@string/math"
                        android:layout_gravity="center_vertical"
                        android:layout_marginStart="70dp"/>
                    <TextView
                        android:id="@+id/english"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="24sp"
                        android:text="@string/english"
                        android:layout_gravity="center_vertical"
                        android:layout_marginStart="70dp"/>
                </LinearLayout>

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


        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:overScrollMode="always"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:text="@string/test_text"
                    android:textSize="50sp" />
            </LinearLayout>

        </androidx.core.widget.NestedScrollView>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>


</LinearLayout>

 

 

4、 运行结果:

引入后运行正常:

未触发
触发上滑触顶悬浮
  • 13
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android RecyclerView 实现滑动悬停是通过使用 ItemDecoration 来实现的。在 RecyclerView 中滑动悬停需要满足两个条件:一是要有一个能够进行悬停的 View,二是要能够动态地根据 RecyclerView 的滚动来改变悬停 View 的位置。 实现滑动悬停的步骤如下: 1. 首先,我们需要创建一个继承自 RecyclerView.ItemDecoration 的类,例如名为 StickyHeaderDecoration 的类,来实现悬停 View。 2. 在 StickyHeaderDecoration 类中,我们需要重写 getItemOffsets() 方法,该方法会在每次绘制 RecyclerView 的子项时被调用。在该方法中,我们可以根据子项的位置和需要悬停的条件来判断是否需要为该子项添加偏移量,从而实现悬停的效果。 3. 接下来,在 RecyclerView 的 Adapter 类中,我们需要重写两个方法:getItemViewType() 和 onCreateHolder()。 4. 在 getItemViewType() 方法中,我们可以根据当前子项的位置来判断是否需要为该子项设置一种特殊的 ViewType,用于标识悬停 View。例如当子项为要悬停的位置时,我们可以返回一个特定的 ViewType 值。 5. 在 onCreateHolder() 方法中,我们需要根据 ViewType 的不同创建不同的 ViewHolder。例如当 ViewType 为悬停 View 的类型时,我们可以创建一个单独的 ViewHolder 类来确定悬停 View 的样式。 6. 最后,在 RecyclerView 的布局文件中,我们需要将 StickyHeaderDecoration 添加到 RecyclerView 中,并设置它的布局参数。 通过以上步骤,我们就可以实现滑动悬停的效果了。当滚动 RecyclerView 时,悬停 View 会根据 RecyclerView 的滚动位置动态地改变自己的位置,从而实现滑动悬停的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值