2015-02-03 2 views
1

Я хочу создать перетаскиваемый вид, который можно перетаскивать только на оси Y. Обычно я хотел бы использовать DragShadow и будет делать что-то вроде этого:Как перетащить вид ТОЛЬКО на ось y?

@Override 
public boolean onTouch(View view, MotionEvent motionEvent) { 
    switch (motionEvent.getAction()) { 
    case MotionEvent.ACTION_DOWN: { 
     View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 

     view.startDrag(null, shadowBuilder, view, 0); 
     view.setVisibility(View.INVISIBLE); 
    } break; 

    return true; 
} 

@Override 
public boolean onDrag(View view, DragEvent dragEvent) { 
    View sourceView = (View) dragEvent.getLocalState(); 

    float sourceX = sourceView.getX(); 
    float sourceY = sourceView.getY(); 
    float dropX = dragEvent.getX() - (sourceView.getWidth()/2); 
    float dropY = dragEvent.getY() - (sourceView.getHeight()/2); 

    switch(dragEvent.getAction()) { 
    case DragEvent.ACTION_DRAG_EXITED : { 
     TranslateAnimation animation = new TranslateAnimation(dropX - sourceX, 0, dropY - sourceY, 0); 
     animation.setDuration(300); 

     sourceView.startAnimation(animation); 
     sourceView.setX(sourceX); 
     sourceView.setY(sourceY); 
     sourceView.setVisibility(View.VISIBLE); 
    } break; 
    case DragEvent.ACTION_DROP : { 
     sourceView.setX(dropX); 
     sourceView.setY(dropY); 
     sourceView.setVisibility(View.VISIBLE); 

     // TranslateAnimation animation = new TranslateAnimation(dropX - sourceX, 0, dropY - sourceY, 0); 
     // animation.setDuration(300); 

     // sourceView.startAnimation(animation); 
     // sourceView.setX(sourceX); 
     // sourceView.setY(sourceY); 
    } break; 
    } 
} 

Проблема заключается в том, что я не хочу, чтобы View или DragShadow быть dragable на оси X. Положение X всегда должно оставаться неизменным, разрешается изменять только Y-ось.

Как я могу это сделать?

ответ

0

Простой трюк! Пользователь VerticalSeekBar:

public class VerticalSeekBar extends SeekBar { 
    /** 
    * 
    */ 
    private OnSeekBarChangeListener onChangeListener = null; 
    /** 
    * 
    */ 
    private int lastProgress = 0; 

    /** 
    * 
    * @param context 
    */ 
    public VerticalSeekBar(Context context) { 
     super(context); 
    } 

    /** 
    * 
    * @param context 
    * @param attrs 
    * @param defStyle 
    */ 
    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    /** 
    * 
    * @param context 
    * @param attrs 
    */ 
    public VerticalSeekBar(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    /** 
    * 
    */ 
    @Override 
    protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) { 
     super.onSizeChanged(height, width, oldHeight, oldWidth); 
    } 

    /** 
    * 
    */ 
    @Override 
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(heightMeasureSpec, widthMeasureSpec); 

     this.setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); 
    } 

    /** 
    * 
    */ 
    protected void onDraw(Canvas c) { 
     c.rotate(-90); 
     c.translate(-getHeight(), 0); 

     super.onDraw(c); 
    } 

    /** 
    * 
    */ 
    @Override 
    public void setOnSeekBarChangeListener(OnSeekBarChangeListener onChangeListener){ 
     this.onChangeListener = onChangeListener; 
    } 

    /** 
    * 
    */ 
    @SuppressLint("ClickableViewAccessibility") 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (!isEnabled()) { 
      return false; 
     } 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      this.onChangeListener.onStartTrackingTouch(this); 

      this.setPressed(true); 
      this.setSelected(true); 

      break; 
     case MotionEvent.ACTION_MOVE: 
      super.onTouchEvent(event); 

      int progress = getMax() - (int) (getMax() * event.getY()/getHeight()); 
      if(progress < 0) { progress = 0; } 
      if(progress > getMax()) { progress = getMax(); } 

      this.setProgress(progress); 

      if(progress != this.lastProgress) { 
       this.lastProgress = progress; 
       this.onChangeListener.onProgressChanged(this, progress, true); 
      } 

      this.onSizeChanged(getWidth(), getHeight() , 0, 0); 
      this.setPressed(true); 
      this.setSelected(true); 

      break; 
     case MotionEvent.ACTION_UP: 
      this.onChangeListener.onStopTrackingTouch(this); 

      this.setPressed(false); 
      this.setSelected(false); 

      break; 
     case MotionEvent.ACTION_CANCEL: 
      super.onTouchEvent(event); 

      this.setPressed(false); 
      this.setSelected(false); 

      break; 
     } 

     return true; 
    } 

    /** 
    * 
    * @param progress 
    */ 
    public synchronized void setProgressAndThumb(int progress) { 
     this.setProgress(progress); 
     this.onSizeChanged(getWidth(), getHeight() , 0, 0); 

     if(progress != this.lastProgress) { 
      this.lastProgress = progress; 
      this.onChangeListener.onProgressChanged(this, progress, true); 
     } 
    } 

    /** 
    * 
    * @return 
    */ 
    public synchronized int getMaximum() { 
     return this.getMax(); 
    } 

    /** 
    * 
    * @param maximum 
    */ 
    public synchronized void setMaximum(int maximum) { 
     this.setMax(maximum); 
    } 
} 
Смежные вопросы