自定義View練習——ScrollView嵌套聯(lián)動

當我們有兩個ScrollView嵌套時,通常需要解決事件沖突。不然當內(nèi)層的ScrollView因為內(nèi)容展示不完整時,比如內(nèi)層的內(nèi)容長度是1000,但是內(nèi)層的ScrollView的viewport則只有500時,那么在嵌套ScrollView時,內(nèi)層的ScrollView是無法滑動查看自己的內(nèi)容的。若要讓內(nèi)層的ScrollView可以滑動,那么我們可以在內(nèi)層的ScrollView的事件攔截中,通過強制要求父布局,也就是外層的ScrollView不要處理該事件,就可以解決這個問題。


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.onInterceptTouchEvent(ev);
    }

但是這樣的效果有點僵硬,當用戶滑動內(nèi)層ScrollView到內(nèi)容的上下邊緣時,其實是想要讓外層ScrollView能夠接著繼續(xù)滑動,而不是抬起手指再次滑動。也就存在兩種情況:

  • 1:當InnerScrollView的內(nèi)容mScrollY是0時,允許用戶向下滑動內(nèi)層scrollView時,其實聯(lián)動外層scrollView跟著滑動。
  • 2:當InnerScrollView的內(nèi)容mScrollY達到(內(nèi)容高度-視窗高度)這個距離時,允許用戶向上滑動內(nèi)層scrollView,并且讓外層scrollView跟著滑動。

那么我們要實現(xiàn)的目標如下:


scrollView.gif

思路上面已經(jīng)很清楚了,那么就可以根據(jù)內(nèi)部攔截法來實現(xiàn)代碼如下:

 private  int lastY;
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
         int  currentY= (int) ev.getY();
        switch (ev.getAction()){
            case MotionEvent.ACTION_MOVE:
                int i = currentY - lastY;
                //判斷方向
                if(i>0){
                    if(getScrollY()==0){
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                }else{
                    //innerScrollView的可移動高度。
                    if(getScrollY()==(getChildAt(0).getMeasuredHeight()-getHeight())){
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                }
                break;
        }
        lastY = currentY;
        return super.dispatchTouchEvent(ev);
    }

其中g(shù)etScrollY()表示的就是內(nèi)部ScrollView已經(jīng)滑動的距離。

完整代碼如下:
MyScrollView.java

public class MyScrollView extends ScrollView {
    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.onInterceptTouchEvent(ev);
    }

    private  int lastY;
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
         int  currentY= (int) ev.getY();
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int i = currentY - lastY;
                if(i>0){
                    if(getScrollY()==0){
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                }else{
                    //innerScrollView的可移動高度。
                    if(getScrollY()==(getChildAt(0).getMeasuredHeight()-getHeight())){
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                }
                break;
        }
        lastY = currentY;
        return super.dispatchTouchEvent(ev);
    }

}

activity_scrollview_brace_scrollview.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:gravity="center"
                android:textSize="50dp"
                android:text="OuterScrollView"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="#8900ad" />

            <com.goodsnow.view.custom.MyScrollView
                android:layout_width="match_parent"
                android:layout_height="400dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:gravity="center"
                        android:layout_width="match_parent"
                        android:layout_height="30dp"
                        android:text="內(nèi)層scrollView開始" />

                    <TextView
                        android:textSize="50dp"
                        android:gravity="center"
                        android:text="innerScrollView"
                        android:layout_width="match_parent"
                        android:layout_height="500dp"
                        android:background="#554411" />



                    <TextView
                        android:gravity="center"
                        android:layout_width="match_parent"
                        android:layout_height="30dp"
                        android:text="內(nèi)層scrollView結(jié)束" />
                </LinearLayout>
            </com.goodsnow.view.custom.MyScrollView>

            <TextView
                android:text="OuterScrollView"
                android:gravity="center"
                android:textSize="50dp"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="#a60127" />



        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 504b 0304 1400 0008 0800 4498 963d a6fa9ff3 9f41 1e00 a88...
    BossOx閱讀 12,680評論 0 0
  • 504b 0304 1400 0008 0800 fa8c 963d 50740baa dffc 0e00 6cd...
    BossOx閱讀 4,065評論 0 0
  • 年輕人應該怎麼買房子 2016/08/31負債和資產(chǎn)唯一的分界線是流動性,也就是資產(chǎn)變現(xiàn)的容易程度:流動性好的東西...
    chaosystem閱讀 260評論 0 1
  • 安裝環(huán)境:Win10 64位,Python版本 3.7 pip install pyecharts 安裝pyech...
    衣谷文武閱讀 323評論 0 0
  • 熊志軍~【日精進打卡第347天】 4月27號卡 付達新商貿(mào)~眾德營銷 沈陽盛和塾道盛組/稻芽七組 【知~學習】 誦...
    熊志軍閱讀 302評論 0 0

友情鏈接更多精彩內(nèi)容