2012-03-20 16 views
10

Czy ruch paska podporowego jest możliwy tylko wtedy, gdy kciuk jest przesuwany. W tej chwili pasek wyszukiwania przesuwa się nawet po dotknięciu palcem w postępie do przesunięcia . W jaki sposób wyłączyć ruch paska wyszukiwania na dotknięciu przycisku progressdrawable?Rozwiązanie Android seekbar

Dzięki.

+0

gdzie jesteś w stanie to osiągnąć? Mam ten sam problem pilnie potrzebnej pomocy –

+0

@Jay: HI, mam ten sam problem jak ty znalazłeś jakieś rozwiązanie dla tego? Proszę podzielić się z nami, jeśli masz rozwiązanie tego ... – Rishi

Odpowiedz

2

Nadpisz OnTouchListener dla SeekBar i jedynym procesem ruch na kciuku, gdy MotionEvent jest zdarzeniem przenoszenia.

event.getAction() == MotionEvent.ACTION_MOVE

Aktualizacja: 1

Coś jak to będzie działać, ale haczyk jest to, że nawet wtedy, gdy użytkownik przesuwa kciukiem 2 agregaty moves SeekBar. I nie powinieneś naprawdę powstrzymywać tego zachowania, ponieważ mogłoby to zepsuć pasek wyszukiwania.

seekBar.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if(event.getAction() == MotionEvent.ACTION_MOVE){ 
        Log.d(TAG, "Moved , process data, Moved to :" + seekBar.getProgress()); 
        seekBar.setProgress(seekBar.getProgress()); 
        return false; 
       } 
       Log.d(TAG, "Touched , Progress :" + seekBar.getProgress()); 
       return true; 
      } 
     }); 
+0

Hie, jest to jakoś przydatne dla mnie .. ale nadal, gdy klikam na seekbara, progres wzrasta bez ruchu lub dotyku do kciuka .. możesz podać mi pełne szczegóły, proszę? –

+0

zaktualizowałeś odpowiedź :) –

+1

Hej, ale czasami nie działa .. –

0

Czy spojrzał na to:

Naprawdę podobny wątek, który powinien pomóc rozwiązać pytanie:

SeekBar's thumb only appears when touched

powodzenia

+0

Cześć, nie chcę zniknąć kciuka, po prostu chcę wyłączyć pułapkę zdarzenia .. użytkownik nie może dotknąć na pasku wyszukiwania n postępu postępu. postęp można zwiększyć tylko wtedy, gdy poruszę kciukiem. Dzięki –

+0

Teraz co się dzieje? Kiedy klikam na region apletu w dowolnym miejscu na nim bez dotykania n przesuń do kciuka, kciuk automatycznie się poruszy, jest źle ze mną..użytkownik może się rozwijać tylko przy dotknięciu n ruchu na kciuku. –

7

okazało się, że problem z Ravi's solution jest to, że dotyka i poruszający się poza aktualnej pozycji kciuka będzie nadal prowadzić w skoku.

Poniższa klasa rozwiązuje ten problem i zastępuje skok po dotyku niewielkim krokiem, taki sam, jak w przypadku klawiszy strzałek.


import android.content.Context; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.view.KeyEvent; 
import android.view.MotionEvent; 
import android.widget.SeekBar; 

/** 
* A NoSkipSeekBar is an extension of {@link SeekBar} that prevents jumps in position 
* by touching outside the current thumb position. Such touches are replaced by 
* an increment or decrement the same as would be achieved using a DPAD's Left or 
* Right arrow keys. 
*/ 
public class NoSkipSeekBar extends SeekBar { 

    public NoSkipSeekBar(Context context) { 
     super(context); 
    } 

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

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

    private boolean isDragging; 

    private boolean isWithinThumb(MotionEvent event) { 
     return getThumb().getBounds().contains((int)event.getX(), (int)event.getY()); 
    } 

    private void increment(int direction) { 
     if (direction != 0) { 
      final KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, 
        direction < 0 ? KeyEvent.KEYCODE_DPAD_LEFT : KeyEvent.KEYCODE_DPAD_RIGHT); 
      onKeyDown(key.getKeyCode(), key); 
     } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (!isEnabled() || getThumb() == null) return super.onTouchEvent(event); 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      if (isWithinThumb(event)) { 
       isDragging = true; 
       return super.onTouchEvent(event); 
      } else { 
       return true; 
      } 

     case MotionEvent.ACTION_UP: 
      isDragging = false; 
      if (isWithinThumb(event)) { 
       return super.onTouchEvent(event); 
      } else { 
       final Rect r = getThumb().getBounds(); 
       increment((int)event.getX() - (r.left + r.right)/2); 
       return true; 
      } 

     case MotionEvent.ACTION_MOVE: 
      if (!isDragging) return true; 
      break; 

     case MotionEvent.ACTION_CANCEL: 
      isDragging = false; 
      break; 
     } 

     return super.onTouchEvent(event); 
    } 
} 
+1

dzięki to mi pomogło, to wygląda na rozwiązanie nie tymczasowe obejście, takie jak zaakceptowana odpowiedź, +1 dla tego – serine

+0

działa idealnie - dobra odpowiedź –

0

rozwiązanie Raviego jest super, zrobiłem trochę poniżej refaktoryzacji:

dwóch celach:

  1. rozwiązać funkcję getThumb() że tylko można użyć API 16 powyżej zagadnień.
  2. klikając SeekBar nie będzie powoływać się na onStopTrackingTouch() ale tylko przeciągnąć kciukiem

To jest mój kod:

public class NoSkipSeekBar extends SeekBar { 

    Drawable mThumb; 
    @Override 
    public void setThumb(Drawable thumb) { 
     super.setThumb(thumb); 
     mThumb = thumb; 
    } 
    public Drawable getSeekBarThumb() { 
     return mThumb; 
    } 

    public NoSkipSeekBar(Context context) { 
     super(context); 
    } 

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

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

    private boolean isDragging; 
    private boolean isStart = false; 
    private boolean isWithinThumb(MotionEvent event) { 
     Rect rect = getSeekBarThumb().getBounds();//increate the thumb invoke area 
     Rect rect1 = new Rect(); 
     rect1.left = rect.left - 50; 
     rect1.right = rect.right + 50; 
     rect1.bottom = rect.bottom + 50; 
     return rect1.contains((int)event.getX(), (int)event.getY()); 
    } 

    private void increment(int direction) { 
     if (direction != 0) { 
      final KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, 
        direction < 0 ? KeyEvent.KEYCODE_DPAD_LEFT : KeyEvent.KEYCODE_DPAD_RIGHT); 
      onKeyDown(key.getKeyCode(), key); 
     } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (!isEnabled() || getSeekBarThumb() == null) return super.onTouchEvent(event); 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       if (isWithinThumb(event)) { 
        isDragging = true; 
        isStart = true; 
        return super.onTouchEvent(event); 
       } else { 
        return true; 
       } 

      case MotionEvent.ACTION_UP: 
       isDragging = false; 
       if(isStart){ 
        isStart = false; 
        return super.onTouchEvent(event); 
       } 
       if (isWithinThumb(event)) { 
        return super.onTouchEvent(event); 
       } else { 
        //final Rect r = getThumb().getBounds(); 
        //increment((int)event.getX() - (r.left + r.right)/2); 
        return true; 
       } 
      case MotionEvent.ACTION_MOVE: 
       if (!isDragging) return true; 
       break; 

      case MotionEvent.ACTION_CANCEL: 
       isDragging = false; 
       break; 
     } 

     return super.onTouchEvent(event); 
    } 
} 
Powiązane problemy