自定义View练习——ScrollView嵌套联动

当我们有两个ScrollView嵌套时,通常需要解决事件冲突。不然当内层的ScrollView因为内容展示不完整时,比如内层的内容长度是1000,但是内层的ScrollView的viewport则只有500时,那么在嵌套ScrollView时,内层的ScrollView是无法滑动查看自己的内容的。若要让内层的ScrollView可以滑动,那么我们可以在内层的ScrollView的事件拦截中,通过强制要求父布局,也就是外层的ScrollView不要处理该事件,就可以解决这个问题。


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

但是这样的效果有点僵硬,当用户滑动内层ScrollView到内容的上下边缘时,其实是想要让外层ScrollView能够接着继续滑动,而不是抬起手指再次滑动。也就存在两种情况:

  • 1:当InnerScrollView的内容mScrollY是0时,允许用户向下滑动内层scrollView时,其实联动外层scrollView跟着滑动。
  • 2:当InnerScrollView的内容mScrollY达到(内容高度-视窗高度)这个距离时,允许用户向上滑动内层scrollView,并且让外层scrollView跟着滑动。

那么我们要实现的目标如下:


scrollView.gif

思路上面已经很清楚了,那么就可以根据内部拦截法来实现代码如下:

 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);
    }

其中getScrollY()表示的就是内部ScrollView已经滑动的距离。

完整代码如下:
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="内层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="内层scrollView结束" />
                </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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 504b 0304 1400 0008 0800 4498 963d a6fa9ff3 9f41 1e00 a88...
    BossOx阅读 13,916评论 0 0
  • 504b 0304 1400 0008 0800 fa8c 963d 50740baa dffc 0e00 6cd...
    BossOx阅读 8,855评论 0 0
  • 年輕人應該怎麼買房子 2016/08/31負債和資產唯一的分界線是流動性,也就是資產變現的容易程度:流動性好的東西...
    chaosystem阅读 1,404评论 0 1
  • 安装环境:Win10 64位,Python版本 3.7 pip install pyecharts 安装pyech...
    衣谷文武阅读 1,774评论 0 0
  • 熊志军~【日精进打卡第347天】 4月27号卡 付达新商贸~众德营销 沈阳盛和塾道盛组/稻芽七组 【知~学习】 诵...
    熊志军阅读 1,865评论 0 0