在android问题中的scrollview内的Scrollview

时间:2013-07-16 08:01:25

标签: android

我在scrollview中有一个scrollview。 xml就像这样

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

在第二个滚动视图中不能平滑滚动。可以为此提供解决方案。我在互联网上尝试了很多解决方案但没有工作。

3 个答案:

答案 0 :(得分:21)

试试这段代码。这对我有用。

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

答案 1 :(得分:7)

另一种解决方案是将此类用作父类

public class NoInterceptScrollView extends ScrollView {

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

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

}

答案 2 :(得分: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;
                }
            });
        }
    }
}