7

我在 scrollview 中有一个 scrollview 。xml是这样的

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

在第二个滚动视图中滚动不顺畅。可以为此提供解决方案。我尝试了很多互联网上给出的解决方案,但没有奏效。

4

3 回答 3

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;
}
});`
于 2013-07-16T08:40:47.233 回答
7

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

public class NoInterceptScrollView extends ScrollView {

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

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

}
于 2014-05-27T22:15:43.073 回答
0

我不得不改进 Deepthi 的解决方案,因为它对我不起作用;我猜是因为我的子滚动视图充满了视图(我的意思是子视图使用了所有滚动视图绘图空间)。为了使其功能齐全,我还必须禁止在触摸子滚动视图中的所有子视图时对父滚动的触摸请求:

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;
                }
            });
        }
    }
}
于 2014-12-16T02:37:57.617 回答