繁体   English   中英

Android- Scrollview:通过拖动更改布局位置

[英]Android- Scrollview : change layout position by dragging

我用10个布局创建Scrollview。 我想通过拖动来更改布局位置。

layout_view.setOnTouchListener(new View.OnTouchListener() { 

@Override

public boolean onTouch(View v, MotionEvent ev) {
final int action = ev.getAction();  

switch (action) {   
case MotionEvent.ACTION_DOWN: {
...

问题是当我向下/向上拖动时(当我向右/向左拖动时,它工作正常):

1)发生MotionEvent.ACTION_CANCEL

2)滚动视图正在移动

1)如何在拖动布局时禁用Scrollview滚动?

2)您是否知道如何在不获取MotionEvent.ACTION_CANCEL的情况下保持布局?

谢谢

使用可以启用/禁用的ScrollView替代ScrollView

//A scrollview which can be disabled during drag and drop
public static class OnOffScrollView extends ScrollView {
    private boolean on = true;
    public OnOffScrollView(Context context) {
        super(context);
    }

    public OnOffScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

    //turn on the scroll view
    public void enable() {
        on=true;
    }

    //turn off the scroll view
    public void disable() {
        on = false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (on) {
            return super.onInterceptTouchEvent(ev);
        }
        else {
            return false;
        }
    }
}

在您的MotionEvent.ACTION_DOWN情况下禁用它,在MotionEvent.ACTION_CANCELMotionEvent.ACTION_UP情况下再次启用它

暂无
暂无

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

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