2010-08-30 82 views
2

我使用10佈局創建滾動查看。 我想通過拖動來改變佈局位置。Android-滾動查看:通過拖動來更改佈局位置

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: { 
... 

問題是,當我拖着DOWN/UP(當我拖着左/右它的工作完美):

1)MotionEvent.ACTION_CANCEL發生

2)滾動型移動

1)如何在拖動佈局時禁用滾動查看滾動?

2)你有任何想法如何在沒有獲得MotionEvent.ACTION_CANCEL的情況下保持佈局嗎?

感謝

+0

這不是一個功能據我所知,這將很容易實現(如果有的話)。您可以嘗試將scrollview設置爲不可聚焦。 – Falmarri 2010-08-30 10:22:44

回答

1

覆蓋滾動型與一個你可以啓用/禁用

//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_CANCELMotionEvent.ACTION_UP箱子禁用它在你的MotionEvent.ACTION_DOWN情況下,啓用它