您的位置:首页 > 其它

ScrollView嵌套滑动冲突问题解决

2016-09-26 18:14 183 查看
package com.example.epnc.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.ScrollView;

/**
* Created by TCL on 2016/9/2.
* 屏蔽外层滑动事件的类
*/
public class InnerScrollView extends ScrollView {
public InnerScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public InnerScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public InnerScrollView(Context context) {
super(context);
}

int oldX = 0;

@Override
public boolean onTouchEvent(MotionEvent ev) {

switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
oldX = (int) ev.getX();
break;
case MotionEvent.ACTION_MOVE:
int newX = (int) ev.getX();
View view = (View) getParent();
while (!(view instanceof ScrollView) && !(view instanceof ListView)) {//暴力查找
view = (View) view.getParent();
if (view == null) {
break;
}
}
if (Math.abs(newX - oldX) > 50) {
//拦截
requestDisallowInterceptTouchEvent(false);
break;
}

if (getScrollY() + getHeight() >= computeVerticalScrollRange() + 1) {
//拦截,父响应
requestDisallowInterceptTouchEvent(false);
} else {

//不拦截,事件给子view
requestDisallowInterceptTouchEvent(true);
}
break;
}
return super.onTouchEvent(ev);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ScrollView 滑动冲突
相关文章推荐