首页 潮人

Android带有粘性头部的ScrollView

时间:2018-08-08 06:51:48 栏目:潮人
点击上方“Android编程精选”,选择“置顶公众号”

关键时刻,第一时间送达!Android带有粘性头部的ScrollView

Android带有粘性头部的ScrollView


作者: WelliJohn  來源:51CTO博客

 http://blog.51cto.com/7091572/2066690

Android编程精选整理发布,转载请联系作者获得授权


前言,一天在点外卖的时候,注意到饿了么列表页的滑动效果不错,但是觉得其中的手势滑动还是挺复杂的,正好又碰到了在熟悉Touch事件的理解当中,所以就抽空对着饿了么的列表页面尝试写写这个效果

1.先贴一个实现的效果图

逻辑是当外部的ScrollView没有滑到底部的时候,往上滑动的时候,是滑动外部的ScrollView,当外部的ScrollView到达底部的时候,我们再网上滑,就是滑动内部的列表了,另外在左右滑动的时候,当左右滑动的距离大于minPageSlop的话,那么就执行左右滑动。
如下是仿饿了么的列表页的效果图:

Android带有粘性头部的ScrollView

2.引入

在项目根目录的build.gradle文件下增加jitpack的repo地址
allprojects {
 repositories {
    jcenter()
    maven { url "https://jitpack.io" }
 }
}

在需要引入的module中引入library
dependencies {
    implementation 'com.github.WelliJohn:StickScrollView:0.0.3'
}

3.界面的布局说明

    <wellijohn.org.stickscrollview.ScrollViewWithStickHeader
        android:id="@+id/stick_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:descendantFocusability="blocksDescendants"
            android:focusableInTouchMode="true"
            android:orientation="vertical">
            //这里是header部分,可以随便自定义
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll_stick_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <android.support.design.widget.TabLayout
                    android:id="@+id/order_manager_tabs"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:background="#FFFFFF"
                    tools:tabGravity="fill"
                    tools:tabMode="fixed" />

                <android.support.v4.view.ViewPager
                    android:id="@+id/vp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </LinearLayout>
    </wellijohn.org.stickscrollview.ScrollViewWithStickHeader>

比如我们看到的仿饿了么的列表页界面,我们就需要在ViewPager设置Fragment,fragment中是左右两个列表,看下fragment的xml设置:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <wellijohn.org.stickscrollview.ChildRecyclerView
        android:id="@+id/child_recyclerview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#EEEEEE" />

    <wellijohn.org.stickscrollview.ChildRecyclerView
        android:id="@+id/child_recyclerview_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:layout_weight="3" />
</LinearLayout>

4.注意事项


5.0.0.3版本修复当有底部有操作栏的时候,界面的滚动出现错乱的问题。

当我们底部有view需要固定的时候,我们需要通过

mStickScrollView.setBottomView(mViewBottom);就可以了,如下所示:

Android带有粘性头部的ScrollView

6.任何控件的使用我们最好都知道它的实现方式,所以在这里简单介绍下这款控件的设计思路(ChildScrollView,ChildRecyclerView,ChildWebView下面的都称为子ScrollView)?

父ScrollVIew子ScrollView手势滑动方向滑动事件交由哪个view控制
不在底部顶部向上父ScrollView
不在底部顶部向下父ScrollView
底部不在顶部向上子ScrollView
底部不在顶部向下子ScrollView
底部顶部向下父ScrollView
底部顶部向上子ScrollView

在这里当父ScrollView不在底部的时候,不会出现子ScrollView不在顶部的情况,所以在这里就不分析了。

7.github地址

相关文章