前言,一天在點外賣的時候,注意到餓了麼列表頁的滑動效果不錯,可是以爲其中的手勢滑動仍是挺複雜的,正好又碰到了在熟悉Touch事件的理解當中,因此就抽空對着餓了麼的列表頁面嘗試寫寫這個效果android
邏輯是當外部的ScrollView沒有滑到底部的時候,往上滑動的時候,是滑動外部的ScrollView,當外部的ScrollView到達底部的時候,咱們再網上滑,就是滑動內部的列表了,另外在左右滑動的時候,當左右滑動的距離大於minPageSlop的話,那麼就執行左右滑動。 以下是仿餓了麼的列表頁的效果圖: git
在項目根目錄的build.gradle文件下增長jitpack的repo地址 allprojects { repositories { jcenter() maven { url "https://jitpack.io" } } } 在須要引入的module中引入library dependencies { implementation 'com.github.WelliJohn:StickScrollView:0.0.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>
當咱們底部有view須要固定的時候,咱們須要經過mStickScrollView.setBottomView(mViewBottom);就能夠了,以下所示:maven
父ScrollVIew | 子ScrollView | 手勢滑動方向 | 滑動事件交由哪一個view控制 |
---|---|---|---|
不在底部 | 頂部 | 向上 | 父ScrollView |
不在底部 | 頂部 | 向下 | 父ScrollView |
底部 | 不在頂部 | 向上 | 子ScrollView |
底部 | 不在頂部 | 向下 | 子ScrollView |
底部 | 頂部 | 向下 | 父ScrollView |
底部 | 頂部 | 向上 | 子ScrollView |
在這裏當父ScrollView不在底部的時候,不會出現子ScrollView不在頂部的狀況,因此在這裏就不分析了。 |
@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