Skip to content

1.基本功能

donkingliang edited this page Aug 11, 2021 · 3 revisions

基本使用

ConsecutiveScrollerLayout的使用非常简单,把需要滑动的布局作为ConsecutiveScrollerLayout的直接子View即可。

<?xml version="1.0" encoding="utf-8"?>
<com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp">

    </LinearLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.core.widget.NestedScrollView>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/temp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ScrollView>

  <!--  可以嵌套ConsecutiveScrollerLayout  -->
  <com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/design_default_color_primary">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""/>

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

    </com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
</com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout>

注意:

1、ConsecutiveScrollerLayout的作用是协调多个滑动控件的滑动事件,不用把它当作是ScrollView。如果你不需要协调多个滑动控件的滑动事件,或者你只是想让ConsecutiveScrollerLayout包裹的多个控件能滑动,那么就不应该使用ConsecutiveScrollerLayout,使用ScrollView即可。

2、ConsecutiveScrollerLayout只能处理它的直接子View的滑动事件,如果一个滑动控件不是ConsecutiveScrollerLayout的直接子View,而是中间嵌套了其他布局,那么这个滑动控件将无法滑动。需要设置局部滑动或者实现滑动子view的下级view。我们建议滑动控件都应该作为ConsecutiveScrollerLayout的直接子View

3、ConsecutiveScrollerLayout的非滑动子View(如:TextView、LinearLayout等),如果它的内容有可能超出ConsecutiveScrollerLayout的显示高度,就需要使用滑动控件(如ScrollView等)包裹,否则可能内容显示不全。

关于margin

ConsecutiveScrollerLayout支持左右margin,但是不支持上下margin,子View间的间距可以通过Space或者空白View设置。

<?xml version="1.0" encoding="utf-8"?>
<com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical">

    <!--  使用Space设置上下边距   -->
    <Space
        android:layout_width="0dp"
        android:layout_height="20dp" />

    <!--  ConsecutiveScrollerLayout支持左右margin,但是不支持上下margin   -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""/>

    </LinearLayout>

    <!--  使用Space设置上下边距   -->
    <Space
        android:layout_width="0dp"
        android:layout_height="20dp" />

</com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout>

布局对齐方式

ConsecutiveScrollerLayout的布局方式类似于垂直的LinearLayout,但是它没有gravity和子view layout_gravity属性。ConsecutiveScrollerLayout为它的子view提供了layout_align属性,用于设置子view和父布局的对齐方式。layout_align有三个值:左对齐(LEFT)右对齐(RIGHT)中间对齐(CENTER)

<?xml version="1.0" encoding="utf-8"?>
<com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns:android="http://schemas.android.com/apk/res/android"                                                                    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scrollerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical">
  
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_align="LEFT"/> // 对齐方式
  
</com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout>

嵌套Fragment

要想把一个Fragment嵌套在ConsecutiveScrollerLayout里。通常我们需要一个布局容器来承载我们的Fragment,或者直接把Fragment写在activity的布局里。如果Fragment是垂直滑动的,那么承载Fragment的容器需要是ConsecutiveScrollerLayout,以及Fragment的根布局也需要是垂直滑动的。我们推荐使用ConsecutiveScrollerLayout或者其他可垂直滑动的控件(比如:RecyclerView、NestedScrollView)作为Fragment的根布局。如果你的Fragment不是垂直滑动的,则可以忽略这个限制。

<?xml version="1.0" encoding="utf-8"?>
<com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical">

    <!--  承载Fragment的容器  -->
    <com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
  
<!--  MyFragment的根布局是垂直滑动控件  -->
   <fragment
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:name="com.donkingliang.consecutivescrollerdemo.MyFragment"/>

</com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout>

disableChildHorizontalScroll属性

禁用子view的水平滑动,默认为false。如果ConsecutiveScrollerLayout下没有水平滑动的下级view,应该把它设置为true。为true时,将不会分发滑动事件给子view,而是由ConsecutiveScrollerLayout处理,可以优化ConsecutiveScrollerLayout的滑动。

注意:如果你的ConsecutiveScrollerLayout下使用了ViewPager、HorizontalScrollView、水平滑动RecyclerView等,就不要设置disableChildHorizontalScroll为true,因为它会禁止水平滑动。

<?xml version="1.0" encoding="utf-8"?>
<com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    app:disableChildHorizontalScroll="true">

</com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout>
Clone this wiki locally