@@ -30,6 +30,8 @@ implementation 'com.github.donkingliang:ConsecutiveScroller:3.6.0'
3030由于Androidx和Android support包不兼容,所以ConsecutiveScroller使用两个版本分别支持使用Androidx和使用Android support包的项目。
3131大版本号3使用Android support包,大版本号2使用Androidx。
3232
33+ ** 注意:** 如果你准备使用这个库,请务必认真阅读下面的文档。它能让你了解ConsecutiveScrollerLayout可以实现的功能,以及避免不必要的错误。
34+
3335### 基本使用
3436
3537ConsecutiveScrollerLayout的使用非常简单,把需要滑动的布局作为ConsecutiveScrollerLayout的直接子View即可。
@@ -79,10 +81,123 @@ ConsecutiveScrollerLayout的使用非常简单,把需要滑动的布局作为C
7981
8082 </ScrollView >
8183
82- < androidx .recyclerview.widget.RecyclerView
83- android : id = " @+id/recyclerView2 "
84+ <!-- 可以嵌套ConsecutiveScrollerLayout -->
85+ < com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout
8486 android : layout_width =" match_parent"
85- android : layout_height =" wrap_content" />
87+ android : layout_height =" match_parent"
88+ android : background =" @color/design_default_color_primary" >
89+
90+ <TextView
91+ android : layout_width =" match_parent"
92+ android : layout_height =" wrap_content"
93+ android : padding =" 10dp"
94+ android : text =" "
95+ android : textColor =" @android:color/black"
96+ android : textSize =" 18sp" />
97+
98+ <androidx .recyclerview.widget.RecyclerView
99+ android : id =" @+id/recyclerView3"
100+ android : layout_width =" match_parent"
101+ android : layout_height =" match_parent" />
102+
103+ </com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
104+ </com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
105+ ```
106+
107+ #### 关于margin
108+
109+ ConsecutiveScrollerLayout支持左右margin,但是不支持上下margin,子View间的间距可以通过Space设置。
110+
111+ ``` xml
112+ <?xml version =" 1.0" encoding =" utf-8" ?>
113+ <com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns : android =" http://schemas.android.com/apk/res/android"
114+ android : id =" @+id/scrollerLayout"
115+ android : layout_width =" match_parent"
116+ android : layout_height =" match_parent"
117+ android : scrollbars =" vertical" >
118+
119+ <!-- 使用Space设置上下边距 -->
120+ <Space
121+ android : layout_width =" 0dp"
122+ android : layout_height =" 20dp" />
123+
124+ <!-- ConsecutiveScrollerLayout支持左右margin,但是不支持上下margin -->
125+ <LinearLayout
126+ android : layout_width =" match_parent"
127+ android : layout_height =" 200dp"
128+ android : layout_marginLeft =" 20dp"
129+ android : layout_marginRight =" 20dp"
130+ android : background =" @android:color/holo_red_dark"
131+ android : gravity =" center"
132+ android : orientation =" vertical" >
133+
134+ <TextView
135+ android : layout_width =" wrap_content"
136+ android : layout_height =" wrap_content"
137+ android : text =" LinearLayout"
138+ android : textColor =" @android:color/black"
139+ android : textSize =" 18sp" />
140+
141+ </LinearLayout >
142+
143+ <!-- 使用Space设置上下边距 -->
144+ <Space
145+ android : layout_width =" 0dp"
146+ android : layout_height =" 20dp" />
147+
148+ </com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
149+
150+ ```
151+
152+ #### 布局对齐方式
153+
154+ ConsecutiveScrollerLayout的布局方式类似于垂直的LinearLayout,但是它没有gravity和子view layout_gravity属性。ConsecutiveScrollerLayout为它的子view提供了layout_align属性,用于设置子view和父布局的对齐方式。layout_align有三个值:** 左对齐(LEFT)** 、** 右对齐(RIGHT)** 和** 中间对齐(CENTER)** 。
155+
156+ ``` xml
157+ <?xml version =" 1.0" encoding =" utf-8" ?>
158+ <com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns : android =" http://schemas.android.com/apk/res/android" xmlns : app =" http://schemas.android.com/apk/res-auto"
159+ android : id =" @+id/scrollerLayout"
160+ android : layout_width =" match_parent"
161+ android : layout_height =" match_parent"
162+ android : scrollbars =" vertical" >
163+
164+ <TextView
165+ android : layout_width =" match_parent"
166+ android : layout_height =" wrap_content"
167+ android : background =" @android:color/white"
168+ android : padding =" 10dp"
169+ android : text =" 吸顶View - 1"
170+ android : textColor =" @android:color/black"
171+ android : textSize =" 18sp"
172+ app : layout_isSticky =" true"
173+ app : layout_align =" LEFT" /> // 对齐方式
174+
175+ </com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
176+ ```
177+
178+ #### 嵌套Fragment
179+
180+ 要想把一个Fragment嵌套在ConsecutiveScrollerLayout里。通常我们需要一个布局容器来承载我们的Fragment,或者直接把Fragment写在activity的布局里。如果Fragment是垂直滑动的,那么承载Fragment的容器需要是ConsecutiveScrollerLayout,以及Fragment的根布局也需要是垂直滑动的。我们推荐使用ConsecutiveScrollerLayout或者其他可垂直滑动的控件(比如:RecyclerView、NestedScrollView)作为Fragment的根布局。如果你的Fragment不是垂直滑动的,则可以忽略这个限制。
181+
182+ ``` xml
183+ <?xml version =" 1.0" encoding =" utf-8" ?>
184+ <com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns : android =" http://schemas.android.com/apk/res/android"
185+ android : id =" @+id/scrollerLayout"
186+ android : layout_width =" match_parent"
187+ android : layout_height =" match_parent"
188+ android : scrollbars =" vertical" >
189+
190+ <!-- 承载Fragment的容器 -->
191+ <com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout
192+ android : id =" @+id/fragment_container"
193+ android : layout_width =" match_parent"
194+ android : layout_height =" match_parent" />
195+
196+ <!-- MyFragment的根布局是垂直滑动控件 -->
197+ <fragment
198+ android : layout_width =" match_parent"
199+ android : layout_height =" match_parent"
200+ android : name =" com.donkingliang.consecutivescrollerdemo.MyFragment" />
86201
87202</com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
88203```
@@ -172,6 +287,38 @@ ConsecutiveScrollerLayout的使用非常简单,把需要滑动的布局作为C
172287</com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
173288```
174289
290+ #### 常驻吸顶
291+
292+ 如果你不希望吸顶的view被后面的吸顶view顶出屏幕,而且多个吸顶view排列吸附在顶部,你可以设置常驻吸顶模式:** app: isPermanent ="true"** 。
293+
294+ ``` xml
295+ <?xml version =" 1.0" encoding =" utf-8" ?>
296+ <com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns : android =" http://schemas.android.com/apk/res/android"
297+ xmlns : app =" http://schemas.android.com/apk/res-auto"
298+ android : id =" @+id/scrollerLayout"
299+ android : layout_width =" match_parent"
300+ android : layout_height =" match_parent"
301+ app : isPermanent =" true" // 常驻吸顶
302+ android : scrollbars =" vertical" >
303+
304+ </com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
305+ ```
306+
307+ #### 关于吸顶功能的其他方法
308+
309+ ``` java
310+ // 设置吸顶到顶部的距离,在距离顶部一定距离时开始悬停吸顶
311+ scrollerLayout. setStickyOffset(50 );
312+
313+ // 监听吸顶变化(普通模式)
314+ scrollerLayout. setOnStickyChangeListener(OnStickyChangeListener );
315+ // 监听吸顶变化(常驻模式)
316+ scrollerLayout. setOnPermanentStickyChangeListener(OnPermanentStickyChangeListener );
317+ // 获取当前吸顶view(普通模式)
318+ scrollerLayout. getCurrentStickyView();
319+ // 获取当前吸顶view(常驻模式)
320+ scrollerLayout. getCurrentStickyViews();
321+ ```
175322
176323### 局部滑动
177324
@@ -238,6 +385,7 @@ ConsecutiveScrollerLayout支持NestedScrolling机制,如果你的局部滑动
238385### 滑动子view的下级view
239386
240387ConsecutiveScrollerLayout默认情况下只会处理它的直接子view的滑动,但有时候需要滑动的布局可能不是ConsecutiveScrollerLayout的直接子view,而是子view所嵌套的下级view。比如ConsecutiveScrollerLayout嵌套FrameLayout,FrameLayout嵌套ScrollView,我们希望ConsecutiveScrollerLayout也能正常处理ScrollView的滑动。为了支持这种需求,ConsecutiveScroller提供了一个接口:IConsecutiveScroller。子view实现IConsecutiveScroller接口,并通过实现接口方法告诉ConsecutiveScrollerLayout需要滑动的下级view,ConsecutiveScrollerLayout就能正确地处理它的滑动事件。IConsecutiveScroller需要实现两个方法:
388+
241389``` java
242390 /**
243391 * 返回当前需要滑动的下级view。在一个时间点里只能有一个view可以滑动。
@@ -249,7 +397,9 @@ ConsecutiveScrollerLayout默认情况下只会处理它的直接子view的滑动
249397 */
250398 List<View > getScrolledViews();
251399```
400+
252401在前面提到的例子中,我们可以这样实现:
402+
253403``` java
254404public class MyFrameLayout extends FrameLayout implements IConsecutiveScroller {
255405
@@ -268,6 +418,7 @@ public class MyFrameLayout extends FrameLayout implements IConsecutiveScroller {
268418 }
269419}
270420```
421+
271422``` xml
272423<?xml version =" 1.0" encoding =" utf-8" ?>
273424<com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns : android =" http://schemas.android.com/apk/res/android"
@@ -292,6 +443,7 @@ public class MyFrameLayout extends FrameLayout implements IConsecutiveScroller {
292443 </com .donkingliang.consecutivescrollerdemo.widget.MyFrameLayout>
293444</com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
294445```
446+
295447这样ConsecutiveScrollerLayout就能正确地处理ScrollView的滑动。这是一个简单的例子,在实际的需求中,我们一般不需要这样做。
296448
297449** 注意:**
@@ -301,10 +453,53 @@ public class MyFrameLayout extends FrameLayout implements IConsecutiveScroller {
3014532、滑动的控件应该跟嵌套它的子view的高度保持一致,也就是说滑动的控件高度应该是match_parent,并且包裹它的子view和它本身都不要设置上下边距(margin和ppadding)。宽度没有这个限制。
302454
303455#### 对ViewPager的支持
456+
304457如果你的ViewPager承载的子布局(或Fragment)不是可以垂直滑动的,那么使用普通的ViewPager即可。如果是可以垂直滑动的,那么你的ViewPager需要实现IConsecutiveScroller接口,并返回需要滑动的view对象。框架里提供了一个实现了IConsecutiveScroller接口自定义控件:** ConsecutiveViewPager** 。使用这个控件,然后你的ConsecutiveViewPager的子view(或Fragment的根布局)是可垂直滑动的view,如:RecyclerView、NestedScrollView、ConsecutiveScrollerLayout即可。这样你的ViewPager就能正确地跟ConsecutiveScrollerLayout一起滑动了。
305458
459+ ``` xml
460+ <?xml version =" 1.0" encoding =" utf-8" ?>
461+ <com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns : android =" http://schemas.android.com/apk/res/android"
462+ xmlns : app =" http://schemas.android.com/apk/res-auto"
463+ android : id =" @+id/scrollerLayout"
464+ android : layout_width =" match_parent"
465+ android : layout_height =" match_parent"
466+ android : scrollbars =" vertical" >
467+
468+ <com .google.android.material.tabs.TabLayout
469+ android : id =" @+id/tabLayout"
470+ android : layout_width =" match_parent"
471+ android : layout_height =" wrap_content"
472+ android : background =" @android:color/white"
473+ app : tabGravity =" fill"
474+ app : tabIndicatorColor =" @color/colorPrimary"
475+ app : tabIndicatorHeight =" 3dp"
476+ app : tabMode =" scrollable"
477+ app : tabSelectedTextColor =" @color/colorPrimary" />
478+
479+ <com .donkingliang.consecutivescroller.ConsecutiveViewPager
480+ android : id =" @+id/viewPager"
481+ android : layout_width =" match_parent"
482+ android : layout_height =" match_parent" />
483+
484+ </com .donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
485+ ```
486+
487+ 布局吸顶时会覆盖在下面的布局的上面,有时候我们希望TabLayout吸顶悬浮在顶部,但是不希望它覆盖遮挡ViewPager的内容。ConsecutiveViewPager提供了setAdjustHeight调整自己的布局高度,让自己不被TabLayout覆盖。注意:只有ConsecutiveScrollerLayout是ConsecutiveScrollerLayout的最低部时才能这样做。
488+
489+ ``` java
490+ // 保证能获取到tabLayout的高度
491+ tabLayout. post(new Runnable () {
492+ @Override
493+ public void run () {
494+ viewPager. setAdjustHeight(tabLayout. getHeight());
495+ }
496+ });
497+ ```
498+
306499### 使用腾讯x5的WebView
500+
307501由于腾讯x5的VebView是一个FrameLayout嵌套WebView的布局,而不是一个WebView的子类,所以要在ConsecutiveScrollerLayout里使用它,需要把它的滑动交给它里面的WebView。自定义MyWebView继承腾讯的WebView,重写它的scrollBy()方法即可。
502+
308503``` java
309504public class MyWebView extends com.tencent.smtt.sdk. WebView {
310505
@@ -315,7 +510,9 @@ public class MyWebView extends com.tencent.smtt.sdk.WebView {
315510 }
316511}
317512```
513+
318514通过实现IConsecutiveScroller接口同样可以实现对x5的WebView支持。
515+
319516``` java
320517public class MyWebView extends com.tencent.smtt.sdk. WebView {
321518
@@ -333,7 +530,9 @@ public class MyWebView extends com.tencent.smtt.sdk.WebView {
333530
334531}
335532```
533+
336534另外需要隐藏它的子view的滚动条
535+
337536``` java
338537View view = webView. getView();
339538view. setVerticalScrollBarEnabled(false );
@@ -355,10 +554,18 @@ webView.setWebChromeClient(new WebChromeClient() {
355554 });
356555```
357556
358- 2、继承AbsListView的布局(ListView、GridView等),在滑动上可能会与用户的手指滑动不同步,推荐使用RecyclerView代替。
557+ 2、SmartRefreshLayout和SwipeRefreshLayout等刷新控件可以嵌套ConsecutiveScrollerLayout实现下拉刷新功能,但是ConsecutiveScrollerLayout内部嵌套它们来刷新子view,因为子view时ConsecutiveScrollerLayout滑动内容等一部分。除非你给SmartRefreshLayout或者SwipeRefreshLayout设置app: layout_isConsecutive ="false"。
558+
559+ 3、继承AbsListView的布局(ListView、GridView等),在滑动上可能会与用户的手指滑动不同步,推荐使用RecyclerView代替。
560+
561+ 4、ConsecutiveScroller的minSdkVersion是19,如果你的项目支持19以下,可以设置:
562+
563+ ``` xml
564+ <uses-sdk tools : overrideLibrary =" com.donkingliang.consecutivescroller" />
565+ ```
359566
360- 3、ConsecutiveScrollerLayout的子View不支持margin,子View间的间距可以通过Space设置 。
567+ 但是不要在minSdkVersion小于16的项目使用AbsListView的子类,因为ConsecutiveScrollerLayout使用了只有19以上才有的AbsListView API 。
361568
362- 4 、使用ConsecutiveScrollerLayout提供的setOnVerticalScrollChangeListener()方法监听布局的滑动事件。View所提供的setOnScrollChangeListener()方法已无效。
569+ 5 、使用ConsecutiveScrollerLayout提供的setOnVerticalScrollChangeListener()方法监听布局的滑动事件。View所提供的setOnScrollChangeListener()方法已无效。
363570
364- 5 、通过getOwnScrollY()方法获取ConsecutiveScrollerLayout的垂直滑动距离,View的getScrollY()方法获取的不是ConsecutiveScrollerLayout的整体滑动距离。
571+ 6 、通过getOwnScrollY()方法获取ConsecutiveScrollerLayout的垂直滑动距离,View的getScrollY()方法获取的不是ConsecutiveScrollerLayout的整体滑动距离。
0 commit comments