阿超
>
ConsecutiveScrollerLayout
当面对两个选择时,抛硬币总能奏效,并不是因为它总能给出对的答案,而是在你把它抛在空中的那一秒里,你突然知道你希望它是什么。——曾小贤
https://github.com/donkingliang/ConsecutiveScroller
ConsecutiveScrollerLayout 是一个支持多个滑动布局(如 RecyclerView、ScrollView、ViewPager、WebView 等)和普通控件(如 TextView、ImageView、LinearLayout)的 Android 容器。它的核心功能在于让所有子视图像一个整体一样顺滑地滚动,解决了多层嵌套滑动冲突的问题。它还能实现多种模式的吸顶效果,适应大多数复杂业务场景,支持动态控制吸顶视图的显示状态。
ConsecutiveScrollerLayout 的使用体验非常顺畅,无论是在页面中嵌套多个滚动视图,还是在动态切换视图时,滚动都不会出现明显卡顿或冲突。常见的使用场景包括新闻详情页、商品详情页、仪表盘等内容丰富的页面。
项目中引入这个布局也非常简单。在项目的 build.gradle
文件中配置 JitPack 仓库:
1 2 3 4 5
| allprojects { repositories { maven { url 'https://jitpack.io' } } }
|
接着在模块的 build.gradle
文件中添加依赖:
1
| implementation 'com.github.donkingliang:ConsecutiveScroller:4.6.4'
|
安装依赖后,直接在布局中使用 ConsecutiveScrollerLayout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="200dp" />
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
</LinearLayout> </ScrollView>
</com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
|
为了实现吸顶效果,可以为某个视图添加 layout_isSticky="true"
属性。例如:
1 2 3 4 5 6 7
| <TextView android:layout_width="match_parent" android:layout_height="50dp" android:text="吸顶标题" android:gravity="center" android:background="#FF5722" app:layout_isSticky="true" />
|
在滚动过程中,该视图会固定在页面顶部,直到其下方的内容完全滚动过去。ConsecutiveScrollerLayout 还支持多种吸顶模式,包括默认吸顶、动态吸顶和下沉吸顶,具体效果可以在官方文档和示例中查看。
开发者还可以监听滚动事件,获取当前滚动的坐标:
1 2 3 4 5 6
| consecutiveScrollerLayout.setOnScrollChangeListener(new ConsecutiveScrollerLayout.OnScrollChangeListener() { @Override public void onScrollChange(ConsecutiveScrollerLayout scroller, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { Log.d("Scroll", "当前滚动位置: " + scrollY); } });
|
这种方式在需要实现滚动联动、滚动动画或动态加载内容时非常有用。此外,该布局支持快速返回顶部功能,只需调用 scrollToChild()
方法即可。
ConsecutiveScrollerLayout 在性能方面也表现优异,内存和 CPU 占用较低,即使在复杂布局中也能保持流畅的滚动体验。不过,在实际使用时,尽量避免过深的布局嵌套层级,可以通过懒加载和分页技术进一步优化。
关于更多使用方法和自定义扩展功能,可以参考官方的 使用文档。项目遵循 Apache-2.0 开源协议,允许自由使用和修改源码。对于开发者来说,这是一个在 Android 应用中处理复杂嵌套滑动的理想选择。