繁体   English   中英

Android中滚动视图中的滚动视图

[英]Scrollview inside a scrollview in android issue

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

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

在第二个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;
}
});`

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

public class NoInterceptScrollView extends ScrollView {

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

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

}

我不得不改进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;
                }
            });
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM