2013-07-16 201 views
7

我有一个滚动视图内滚动视图。这个xml就是这样的在android问题的滚动视图内滚动视图

<RelativeLayout .... 
    <ScrollView..... 
     <RelativeLayout .... 
      <Button..... 
      <Button .... 
      <ScrollView 
      <RelativeLayout .... 
       .......... 
       </RelativeLayout> 
      </ScrollView> 
     </RelativeLayout> 
    </ScrollView> 
</RelativeLayout> 

在这第二个scrollview中没有平滑滚动。可以为此提供解决方案。我尝试了很多在互联网上给出的解决方案,但没有工作。

+3

我不认为内滚动视图滚动视图将工作 – KOTIOS

+0

u能告诉我确切requirrrnt – KOTIOS

+2

它不是一个很好的做法,有2个scrollViews在相同的方向相互滚动,即使它工作 –

回答

20

试试看看这个代码。据工作me`

parentScrollView.setOnTouchListener(new View.OnTouchListener() { 

public boolean onTouch(View v, MotionEvent event) 
{ 
    findViewById(R.id.childScrollView).getParent().requestDisallowInterceptTouchEvent(false); 
return false; 
} 
}); 
childScrollView.setOnTouchListener(new View.OnTouchListener() { 

public boolean onTouch(View v, MotionEvent event) 
{ 

// Disallow the touch request for parent scroll on touch of 
// child view 
v.getParent().requestDisallowInterceptTouchEvent(true); 
return false; 
} 
});` 
+0

工作得很好,谢谢! – deimos1988

+0

对我来说,这注意到,childScrollView将不会收到它的ontouch无论我做什么。 – SjoerdvGestel

+0

谢谢它按照预期为我工作。 –

5

不同的解决方案是使用这个类作为

public class NoInterceptScrollView extends ScrollView { 

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

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     return false; 
    } 

} 
+0

我更喜欢这是一个更简单的解决方案 –

+0

你是我的英雄! – Draelach

+0

完美..经过漫长的斗争,我发现这是好的,最好..毫不费力.. – Brendon

0

我不得不提高Deepthi的解决方案,因为它没有为我工作的父类;我猜是因为我的孩子scrollview充满了意见(我的意思是子视图使用所有的scrollview绘图空间)。为了使其充分发挥功能,我只好也不允许对孩子滚动视图内所有儿童的意见触摸母公司滚动触摸请求:

parentScrollView.setOnTouchListener(new View.OnTouchListener() { 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     findViewById(R.id.childScrollView).getParent().requestDisallowInterceptTouchEvent(false); 
     return false; 
    } 
}); 
childScrollView.setOnTouchListener(new View.OnTouchListener() { 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     // Disallow the touch request for parent scroll on touch of 
     // child view 
     v.getParent().requestDisallowInterceptTouchEvent(true); 
     return false; 
    } 
});` 
childScrollviewRecursiveLoopChildren(parentScrollView, childScrollView); 
public void childScrollviewRecursiveLoopChildren(final ScrollView parentScrollView, View parent) { 
    for (int i = ((ViewGroup) parent).getChildCount() - 1; i >= 0; i--) { 
     final View child = ((ViewGroup) parent).getChildAt(i); 
     if (child instanceof ViewGroup) { 
      childScrollviewRecursiveLoopChildren(parentScrollView, (ViewGroup) child); 
     } else { 
      child.setOnTouchListener(new View.OnTouchListener() { 
       public boolean onTouch(View v, MotionEvent event) 
       { 
        // Disallow the touch request for parent scroll on touch of 
        // child view 
        parentScrollView.requestDisallowInterceptTouchEvent(true); 
        return false; 
       } 
      }); 
     } 
    } 
}