Android带有粘性头部的ScrollView

发布时间:2020-06-07 05:33:53 作者:WelliJohn
来源:网络 阅读:16945

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

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

逻辑是当外部的ScrollView没有滑到底部的时候,往上滑动的时候,是滑动外部的ScrollView,当外部的ScrollView到达底部的时候,我们再网上滑,就是滑动内部的列表了,另外在左右滑动的时候,当左右滑动的距离大于minPageSlop的话,那么就执行左右滑动。
如下是仿饿了么的列表页的效果图:
Android带有粘性头部的ScrollViewcdn.xitu.io/2018/1/17/161024c1b081e3b6?w=283&h=500&f=gif&s=1838673">

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地址,您的点赞或者star是我持续开源的最大动力。

推荐阅读:
  1. Android仿qq回弹阻尼ScrollView
  2. Android scrollview监听滑动状态的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

view android 粘性头部

上一篇:Cacti 监控安装

下一篇:黑盒测试之等价类划分

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》