Android之折叠悬浮

    我们的Android手机的屏幕是十分有限的,怎么在有限的屏幕展示足够多的内容一直是我们不懈的追求。而折叠悬浮效果正是这个需求的实现,且兼顾了美观。话不多说,先上效果图。

    内容大致分两部分,当完全展开的时候,上半部分展示标题,且可展示额外的如背景图、头像等信息。下半部分是可滚动的控件或包含可滚动控件的控件(如ViewPager)。而还有一个特殊的部分是当折叠的时候会停留下页面顶部的部分,一般做导航指示器。这样,当完全展开的时候,可以展示足够多的内容。而当完全折叠的时候,有可以将屏幕控件基本全部留给列表,使列表的可展示区域最大化。

    那么这个效果是如何实现的呢,其实是使用了CoordinatorLayout+AppBarLayout+可滚动列表作为核心来实现的。请注意,这里的可滚动列表不能用之前的ListView、GridView,而必须使用实现了NestedScrollingParent、NestedScrollingChild的如NestedScrollView、RecyclerView这种控件。而之所以能实现这种折叠悬浮的效果也正是由于CoordinatorLayout也实现了NestedScrollingParent、NestedScrollingChild,这样它们之间的滚动才能联动起来,知道列表滚动到了哪儿,是否该折叠或者悬浮。

    我这里为了兼容androidX对引入的控件包做了兼容,下面给出包。如果没兼容androidX的话按原控件包引入。

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

先看下布局代码:

<?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=".MainActivity">

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

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="300dp"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.5" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:src="@drawable/ic_sys_notification"/>
            </RelativeLayout>

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

            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.CollapsingToolbarLayout>
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_gravity="center"
            app:layout_scrollFlags="scroll|enterAlways|snap">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="我是想悬停的TableLayout"/>
        </TableLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScrollView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

CoordinatorLayout包裹了一个AppBarLayout和一个NestedScrollView(或者RecyclerView),且滚动控件添加代码app:layout_behavior="@string/appbar_scrolling_view_behavior",AppBarLayout内部控件通过app:layout_scrollFlags来设置联动方式及悬浮。

以上布局就实现了折叠悬浮的页面,基本不需要额外的代码介入。可以发现Google其实已经为我们实现了许多有趣而又好用的控件等待我们去发现呢!

国际惯例,代码地址:https://gitee.com/calm1516/CHover.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android RecyclerView 折叠是指在 RecyclerView 中实现可折叠的列表效果。这种效果通常用于显示大量数据,可以让用户更方便地浏览和管理数据。实现 RecyclerView 折叠需要使用一些特定的布局和适配器,可以根据具体需求进行定制。 ### 回答2: Android RecyclerView是Android的一个强大的控件,它可以展示大量的数据,但是很多时候我们需要折叠或者展开某些数据,这时候就需要使用RecyclerView的折叠功能了。 RecyclerView的折叠功能可以通过使用ExpandableListView来实现。ExpandableListView是一种特殊的ListView,它可以展示分组的数据,每个分组可以有多个子项。我们可以使用ExpandableListView的分组功能来实现RecyclerView的折叠功能。 下面是具体实现步骤: 1.创建RecyclerView的适配器,继承自ExpandableRecyclerViewAdapter。 2.在适配器中实现getGroupCount()、getChildCount()、isGroup()等方法,分别返回分组数、每个分组的子项数和是否为分组。 3.在适配器的ViewHolder中通过findViewById找到分组的标题和子项的内容。 4.在适配器中实现onCreateViewHolder()和onBindViewHolder()方法,将ViewHolder绑定到RecyclerView上。 5.在Activity或Fragment中创建RecyclerView,设置adapter和LayoutManager。 6.为RecyclerView设置ItemDecoration,可以设置分隔线等样式。 7.在Activity或Fragment中实现RecyclerView.OnItemTouchListener接口,实现onInterceptTouchEvent()方法,根据手势判断是否需要展开或折叠。 8.实现折叠效果,根据点击的分组是否展开,来改变分组背景色和子项的显示状态。 以上就是使用ExpandableListView实现RecyclerView折叠效果的步骤,实现起来不难,但是需要注意一些细节,比如ViewHolder的类型等。折叠效果可以让用户更好地浏览数据,提升用户体验。 ### 回答3: Android RecyclerView控件可以用于显示大量数据,同时也支持添加折叠功能,即当用户需要浏览大量的数据时,可以通过折叠控件来隐藏某些数据项,从而节省幕空间,便于用户操作。 使用折叠RecyclerView控件时,需要定义一组带有标题和内容的数据集合,将该数据集合传递给适配器来展示在RecyclerView列表中。其中,数据集合可以用一个自定义的Java类来表示,该类需要包括数据项的标题和内容两个字段,可以使用普通的字符串或自定义数据类型作为数据项的内容。 接下来,在RecyclerView的适配器中,需要覆写getItemViewType(int position)方法,根据数据项类型返回对应的布局类型。在这里,我们可以定义两种不同的布局类型,分别对应标题和内容数据项的布局。 当用户点击某个数据项的标题时,我们需要动态切换内容布局的可见性,从而实现折叠和展开效果。在这里,我们可以将数据项的状态存储在Java类中,添加一个boolean类型的值用于表示该数据项是否已经被折叠。在onBindViewHolder(ViewHolder holder, int position)方法中,我们可以根据数据项的状态来动态设置内容布局的可见性。 最后,在RecyclerView的点击事件处理中,我们可以根据点击位置来判断用户点击的是标题还是内容布局。如果是标题布局,则根据数据项状态动态切换布局的可见性。如果是内容布局,则执行对应的业务逻辑。 总之,Android RecyclerView控件的折叠功能可以通过自定义Java类、实现适配器、覆写布局类型和点击事件处理等步骤来完成。合理利用RecyclerView的折叠功能可以提高用户操作的效率,为用户提供更加友好的使用体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值