SORU
7 Aralık 2010, Salı


Listede Android Tokatlamak

Seni tokatlamak soldan sağa zaman herkes bir ListActivity basit bir örnek, bir sütun Textviews görüntüleme var ve yeni bir görünüm, satır gördünüz mü? Bu satır için verileri söyle düzenlemek veya bu satır ile ilgili daha ayrıntılı bilgi göstermek için olacak. Lütfen Google'dan araştırdım benim gibi kod shogun veya başka sitelere referans yok ve bu cevabı görmedim.

CEVAP
18 ŞUBAT 2012, CUMARTESİ


Aynı sorun bende de vardı ve burada benim cevap bulamadım.

OnİtemClick ve OnİtemLongClick desteklemeye devam ederken, Liste Görünümü öğesi kaydırma eylemine tespit ve çalınmış olarak işaretlemek istedim.

İşte benim çözüm:

1 SwipeDetector sınıfı:

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class SwipeDetector implements View.OnTouchListener {

    public static enum Action {
        LR, // Left to Right
        RL, // Right to Left
        TB, // Top to bottom
        BT, // Bottom to Top
        None // when no action was detected
    }

    private static final String logTag = "SwipeDetector";
    private static final int MIN_DISTANCE = 100;
    private float downX, downY, upX, upY;
    private Action mSwipeDetected = Action.None;

    public boolean swipeDetected() {
        return mSwipeDetected != Action.None;
    }

    public Action getAction() {
        return mSwipeDetected;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            downX = event.getX();
            downY = event.getY();
            mSwipeDetected = Action.None;
            return false; // allow other events like Click to be processed
        case MotionEvent.ACTION_UP:
            upX = event.getX();
            upY = event.getY();

            float deltaX = downX - upX;
            float deltaY = downY - upY;

            // horizontal swipe detection
            if (Math.abs(deltaX) > MIN_DISTANCE) {
                // left or right
                if (deltaX < 0) {
                    Log.i(logTag, "Swipe Left to Right");
                    mSwipeDetected = Action.LR;
                    return false;
                }
                if (deltaX > 0) {
                    Log.i(logTag, "Swipe Right to Left");
                    mSwipeDetected = Action.RL;
                    return false;
                }
            } else if (Math.abs(deltaY) > MIN_DISTANCE) { // vertical swipe
                                                            // detection
                // top or down
                if (deltaY < 0) {
                    Log.i(logTag, "Swipe Top to Bottom");
                    mSwipeDetected = Action.TB;
                    return false;
                }
                if (deltaY > 0) {
                    Log.i(logTag, "Swipe Bottom to Top");
                    mSwipeDetected = Action.BT;
                    return false;
                }
            }
            return false;
        }
        return false;
    }
}

Listede kaydırın dedektörü sınıfı görüntülemek kullanıyorum 2.:

    final ListView lv = getListView();
    final SwipeDetector swipeDetector = new SwipeDetector();
    lv.setOnTouchListener(swipeDetector);
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (swipeDetector.swipeDetected()){
                    // do the onSwipe action 
                } else {
                    // do the onItemClick action
                }
            }
    });
    lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
            if (swipeDetector.swipeDetected()){
                // do the onSwipe action 
            } else {
                // do the onItemLongClick action
            }
        }
    });

3 eylemleri destekliyorum. bu sayede kaydırma, tıklama, uzun tıklama ve Liste Görünümü öğesi bilgi kullanabilirsiniz.

SONRA EKLEDİ:

Liste Görünümü kayan bir eylem yakalar beri, tokatlamak için bazen zor olduğunu. Bunu düzeltmek için, aşağıdaki değişikliği SwipeDetector için yaptım.onTouch:

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            downX = event.getX();
            downY = event.getY();
            mSwipeDetected = Action.None;
            return false; // allow other events like Click to be processed
        }
        case MotionEvent.ACTION_MOVE: {
            upX = event.getX();
            upY = event.getY();

            float deltaX = downX - upX;
            float deltaY = downY - upY;

            // horizontal swipe detection
            if (Math.abs(deltaX) > HORIZONTAL_MIN_DISTANCE) {
                // left or right
                if (deltaX < 0) {
                    Log.i(logTag, "Swipe Left to Right");
                    mSwipeDetected = Action.LR;
                    return true;
                }
                if (deltaX > 0) {
                    Log.i(logTag, "Swipe Right to Left");
                    mSwipeDetected = Action.RL;
                    return true;
                }
            } else 

            // vertical swipe detection
            if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE) {
                // top or down
                if (deltaY < 0) {
                    Log.i(logTag, "Swipe Top to Bottom");
                    mSwipeDetected = Action.TB;
                    return false;
                }
                if (deltaY > 0) {
                    Log.i(logTag, "Swipe Bottom to Top");
                    mSwipeDetected = Action.BT;
                    return false;
                }
            } 
            return true;
        }
    }
    return false;
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • MC JIN'S OLD YouTube CHANNEL

    MC JIN'S OLD

    2 Kasım 2008
  • BigDawsTv

    BigDawsTv

    20 HAZİRAN 2012
  • Jana Williams

    Jana William

    17 AĞUSTOS 2011