BottomSheetBehavior+CoordinatorLayout实现抖音评论列表滑动效果

效果图

《BottomSheetBehavior+CoordinatorLayout实现抖音评论列表滑动效果》 bottomsheet.gif

最近项目有这个需求, 自己能想出的最简单直接的实现办法, 稍稍改动也能支持ListView的情况.

效果说明

  1. 支持列表滑动
  2. 只有当列表滑动到顶端时, 继续下滑, 才会收起整个评论列表. 否则只是评论列表滑动.
  3. 如果设置了评论列表的标题, 可以通过拖动标题直接收起整个评论列表, 这时不再考虑评论列表是否滑动到顶部.

实现说明

所有的都在这个xml布局文件中

<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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/startBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:text="评论列表" />
        
    </RelativeLayout>


    <LinearLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="348dp"
        android:orientation="vertical"
        app:behavior_hideable="true"
        app:behavior_peekHeight="0dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <TextView
            android:id="@+id/list_title_tv"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="#3300ff00"
            android:gravity="center"
            android:text="这个是标题,可以拖住滑动" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="300dp">

        </androidx.recyclerview.widget.RecyclerView>

    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>


完整代码:

https://github.com/shaopx/BottomSheetBehaviorExample

这是示例使用了androidx, 当然如果你不想升级到androidx, 也完全可以使用support库中的控件, 一样的效果. 可以查看这个示例:https://github.com/shaopx/CoordinatorLayoutExample

如果你看了这篇文章, 我猜想你可能对抖音的视频上下页切换播放也赶兴趣, 那么请也看看我的这个小demo
https://github.com/shaopx/MyDouYin
使用exoplayer实现的播放, 预加载, 很流畅

    原文作者:vb12
    原文地址: https://www.jianshu.com/p/1cee88ea0e35
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞