[英]Two nested vertical scroll views in android
我正在使用兩個垂直滾動視圖(例如,父和子)。問題是我無法滾動我的子視圖而不是滾動我的整個父視圖,是否有任何方法可以限制我的父視圖滾動當我滾動我的孩子滾動視圖,反之亦然?
需要幫助...在此先感謝.. !!
您需要處理觸摸事件才能有效地執行此操作
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;
}
});
試試這個吧
注意:這里parentScrollView表示外部ScrollView,而childScrollView表示Innner 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;
}
});
這更適合理解:
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;
}
});
- 這是您可以找到的SOlution與描述在這里。
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;
}
});
//我們必須按照上面的所有子可滾動內容進行操作
這兩種方法可以解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.