首页

源码搜藏网

首页 > 安卓源码 > 技术博客 >

Android开发带有粘性头部的ScrollView

创建时间:2018-01-25 09:15  浏览

Android开发带有粘性头部的ScrollView

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

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

逻辑是当外部的滚动型没有滑到底部的时候,往上滑动的时候,是滑动外部的滚动型,当外部的滚动型到达底部的时候,我们再网上滑,就是滑动内部的列表了,另外在左右滑动的时候,当左右滑动的距离大于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设置片段,片段中是左右两个列表,看下片段的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版本修复当有底部有操作栏的时候,界面的滚动出现错乱的问题。

当我们底部有视图需要固定的时候,我们需要通过mStickScrollView.setBottomView(mViewBottom);就可以了,如下所示:

Android开发带有粘性头部的ScrollView

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

父滚动视图 子滚动型 手势滑动方向 滑动事件交由哪个视图控制
不在底部 顶部 向上 父滚动型
不在底部 顶部 向下 父滚动型
底部 不在顶部 向上 子滚动型
底部 不在顶部 向下 子滚动型
底部 顶部 向下 父滚动型
底部 顶部 向上 子滚动型

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

@Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mScrollViewWithStickHeader == null) return super.onTouchEvent(event);
        int action = event.getAction();

        if (action == MotionEvent.ACTION_DOWN) {
            mLastX = event.getX();
            mLastY = event.getY();
            //首先判断外层ScrollView是否滑动到底部
            if (mScrollViewWithStickHeader.isBottom()) {
                getParent().requestDisallowInterceptTouchEvent(true);
                return super.onTouchEvent(event);
            } else {
                //拦截事件 本身不处理
                getParent().requestDisallowInterceptTouchEvent(false);
                return false;
            }
        }
        if (action == MotionEvent.ACTION_MOVE) {
            float nowY = event.getY();
            if (!mScrollViewWithStickHeader.isBottom() && !isScrolledToTop && nowY - mLastY > 0) {
                if (Math.abs(event.getX() - mLastX) < minPageSlop) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    return super.onTouchEvent(event);
                } else {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            } else if (mScrollViewWithStickHeader.isBottom() && !isScrolledToBottom && nowY - mLastY < 0) {
                if (Math.abs(event.getX() - mLastX) < minPageSlop) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    return super.onTouchEvent(event);
                } else {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            } else if (mScrollViewWithStickHeader.isBottom() && !isScrolledToTop && nowY - mLastY > 0) {
                if (Math.abs(event.getX() - mLastX) < minPageSlop) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    return super.onTouchEvent(event);
                } else {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            } else {
                getParent().requestDisallowInterceptTouchEvent(false);
            }
        }

        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
            getParent().requestDisallowInterceptTouchEvent(false);
        }

        return super.onTouchEvent(event);
    }

这样的话,我们就能实现固定头部的滚动型了。

上一篇:Android开发一个简单的滚动数字的效果实现
下一篇:Android集成添加社会化分享详解

相关内容

热门推荐