android 中的两个嵌套垂直滚动视图
Two nested vertical scroll views in android
我正在使用两个垂直滚动视图(例如,父视图和子视图)。问题是我无法滚动我的子视图,而是滚动我的整个父视图,有什么方法可以限制当我滚动我的子滚动视图时我的父视图滚动,反之亦然?
需要帮助...提前致谢...!!
试试这个
注意:这里parentScrollView表示外层ScrollView,childScrollView表示内层ScrollView
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "PARENT TOUCH");
findViewById(R.id.child_scroll).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "CHILD TOUCH");
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
您需要处理触摸事件才能有效地执行此操作
outerScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
findViewById(R.id.inner_scroll).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
innerScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
-这是您可以找到的解决方案 带有描述Here.
m_parentScrollView.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View p_v, MotionEvent p_event)
{
m_childScrollView.getParent().requestDisallowInterceptTouchEvent(false);
// We will have to follow above for all scrollable contents
return false;
}
});
m_childScrollView.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View p_v, MotionEvent p_event)
{
// this will disallow the touch request for parent scroll on touch of child view
p_v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
// 对于所有子滚动内容,我们必须遵循上面的步骤
我
这两种方法可以解决你的问题。
这样比较好理解:
childScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
parentScrollV.requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
parentScrollV.requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});
我正在使用两个垂直滚动视图(例如,父视图和子视图)。问题是我无法滚动我的子视图,而是滚动我的整个父视图,有什么方法可以限制当我滚动我的子滚动视图时我的父视图滚动,反之亦然?
需要帮助...提前致谢...!!
试试这个
注意:这里parentScrollView表示外层ScrollView,childScrollView表示内层ScrollView
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "PARENT TOUCH");
findViewById(R.id.child_scroll).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "CHILD TOUCH");
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
您需要处理触摸事件才能有效地执行此操作
outerScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
findViewById(R.id.inner_scroll).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
innerScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
-这是您可以找到的解决方案 带有描述Here.
m_parentScrollView.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View p_v, MotionEvent p_event)
{
m_childScrollView.getParent().requestDisallowInterceptTouchEvent(false);
// We will have to follow above for all scrollable contents
return false;
}
});
m_childScrollView.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View p_v, MotionEvent p_event)
{
// this will disallow the touch request for parent scroll on touch of child view
p_v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
// 对于所有子滚动内容,我们必须遵循上面的步骤 我
这两种方法可以解决你的问题。
这样比较好理解:
childScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
parentScrollV.requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
parentScrollV.requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});