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

时间:2010-08-30 09:04:03

标签: android scrollview

我创建了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)Scrollview正在移动

1)当我拖动布局时,如何禁用滚动视图滚动?

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

由于

1 个答案:

答案 0 :(得分:1)

使用可启用/禁用

的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案例中再次启用