Android帶有粘性頭部的ScrollView

前言,一天在點外賣的時候,注意到餓了麼列表頁的滑動效果不錯,可是以爲其中的手勢滑動仍是挺複雜的,正好又碰到了在熟悉Touch事件的理解當中,因此就抽空對着餓了麼的列表頁面嘗試寫寫這個效果android

1.先貼一個實現的效果圖

邏輯是當外部的ScrollView沒有滑到底部的時候,往上滑動的時候,是滑動外部的ScrollView,當外部的ScrollView到達底部的時候,咱們再網上滑,就是滑動內部的列表了,另外在左右滑動的時候,當左右滑動的距離大於minPageSlop的話,那麼就執行左右滑動。 以下是仿餓了麼的列表頁的效果圖: git

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設置:github

<?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.注意事項

  • ScrollViewWithStickHeader內部目前支持放置ViewPager,ScrollView,RecyclerView,WebView
  • ScrollView,RecyclerView,WebView須要對應使用ChildScrollView,ChildRecyclerView,ChildWebView
  • 咱們在使用的時候,須要調用mStickScrollView.setContentView(mContentView);mLLStickList就是咱們須要StickHeader+列表的部分,若是你沒有StickHeader的話,那麼直接設置列表進來也能夠,總之,你想滑動到哪一個位置接下來滑動就是單純下面的部分滑動,那你就把下面的View總體設置爲mContentView。剛剛那個的ContentView是id爲ll_stick_list的View。
  • 另外在這裏ScrollViewWithStickHeader增長autoscroll屬性,默認是關閉的,若是autoscroll:true的話,在咱們手指放開的時候,contentView會判斷是否自動滑動到頂部仍是隱藏不見。 自動滾動的效果圖

5.0.0.3版本修復當有底部有操做欄的時候,界面的滾動出現錯亂的問題。

當咱們底部有view須要固定的時候,咱們須要經過mStickScrollView.setBottomView(mViewBottom);就能夠了,以下所示:maven

6.任何控件的使用咱們最好都知道它的實現方式,因此在這裏簡單介紹下這款控件的設計思路(ChildScrollView,ChildRecyclerView,ChildWebView下面的都稱爲子ScrollView)?

  • 6.1.咱們何時應該讓外部的ScrollView執行滑動事件,何時讓子ScrollView執行滑動。在Android中咱們有一個方法getParent().requestDisallowInterceptTouchEvent(true);就是讓view獲取到對應的事件。
  • 6.2.既然咱們知道了怎麼讓view的touch事件,接下來咱們就要明白在什麼狀況下咱們應該讓父view執行滾動事件,何時讓子view執行滾動事件。以下,我列了表格:
父ScrollVIew 子ScrollView 手勢滑動方向 滑動事件交由哪一個view控制
不在底部 頂部 向上 父ScrollView
不在底部 頂部 向下 父ScrollView
底部 不在頂部 向上 子ScrollView
底部 不在頂部 向下 子ScrollView
底部 頂部 向下 父ScrollView
底部 頂部 向上 子ScrollView
在這裏當父ScrollView不在底部的時候,不會出現子ScrollView不在頂部的狀況,因此在這裏就不分析了。
  • 6.3.分析了,在什麼狀況咱們應該讓子ScrollVIew仍是父ScrollView捕獲滑動事件了,咱們就能夠在咱們的子ScrollView中編寫對應的代碼處理了? 以下面是一段ChildScrollView的onTouchEvent方法的重寫,其餘的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);
    }

這樣的話,咱們就能實現固定頭部的ScrollView了。ide

7.github地址,您的點贊或者star是我持續開源的最大動力。

相關文章
相關標籤/搜索