如何在Android中使用过度滚动模式

4

问题

我需要在我的Activity中给滚动事件设置一些优先级。

我正在使用aiCharts(图表库),并需要在我的区域上进行缩放、平移等操作。如果没有任何ScrollViews,它运作得很好,但是,如果我使用了上述的Layout,这些功能就会出现问题。我认为这是因为视图优先级的原因。

可能的解决方案

我尝试在需要位于ScrollViewHorizontalScrollView“顶部”的视图上使用setOverScrollMode(View.OVER_SCROLL_ALWAYS);,但效果不佳。

布局

 <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:id="@+id/screen_relative_layout"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" >
            </RelativeLayout>        
        </HorizontalScrollView>
    </ScrollView>

我的所有视图都是通过编程方式添加到RelativeLayout中的。

1个回答

0

修改你的RelativeLayout,使android:layout_height="wrap_content" 并创建自定义的ScrollView,以便它仅拦截移动操作而不影响其他操作:

public class VerticalScrollView extends ScrollView {
private float xDistance, yDistance, lastX, lastY;

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

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            xDistance = yDistance = 0f;
            lastX = ev.getX();
            lastY = ev.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            final float curX = ev.getX();
            final float curY = ev.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;
            if(xDistance > yDistance)
                return false;
    }

    return super.onInterceptTouchEvent(ev);
}
}  

来源

请告诉我它是否有效!


如果RelativeLayout不可滚动,它就能正常工作。如果视图尺寸大于屏幕分辨率,则滚动变为活动状态,我会遇到相同的问题。 - Samoji
你需要创建一个自定义 ScrollView,以便只拦截从 x 到 y 的触摸事件。这里有一个示例:https://dev59.com/XHE85IYBdhLWcg3wtV71#2655740还有一个自定义的水平 ScrollView,也只在从左到右和从右到左滑动时拦截触摸事件。希望能对你有所帮助。 - Dyna

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接