Pergunta

Estou implementando um detector onFling muito parecido com a seguinte pergunta:Detecção de gesto de arremesso no layout de grade

No entanto, simplesmente não consigo fazê-lo funcionar.

Caso de alguma forma eu configurei meu aplicativo esteja causando esse problema (talvez o asynctask?), veja como meu aplicativo está estruturado:

(Tentei fazer desta uma lista bem formatada, mas estragou o código abaixo por algum motivo?)

--MapActivity cria um MapView.

--MapView cria um ASyncTask para buscar XML de uma URL.

--onPostExecute() do ASyncTask analisa o XML e adiciona um ItemizedOverlay usando os dados obtidos

A função --onTap() do ItemizedOverlay busca uma imagem da web usando LoaderImageView (http://www.anddev.org/novice-tutorials-f8/imageview-with-loading-spinner-t49439.html)

A função --onTap() então chama a função show() da classe PopupPanel listada abaixo

Como você pode ver no código abaixo, as linhas comentadas funcionam.No entanto, os eventos onTouch e onFling nunca são atingidos quando usados ​​com a classe MyGestureDetector.

class PopupPanel {
    View popup;
    boolean isVisible = false;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    public View.OnTouchListener gestureListener;

    PopupPanel(Context context, int layout) {
        ViewGroup parent = (ViewGroup) map.getParent();

        popup = ((MapActivity) context).getLayoutInflater().inflate(layout, parent, false);

        SelectFilterActivity selectFilterActivity = new SelectFilterActivity();
        popup.setOnClickListener(selectFilterActivity);

// This works!
//            popup.setOnTouchListener(new View.OnTouchListener() {
//                public boolean onTouch(View v, MotionEvent event) {
//                    Toast.makeText(map.getContext(), "Touched", Toast.LENGTH_SHORT).show();
//                    return false;
//                }
//            });

        // Gesture detection
        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(map.getContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(map.getContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    }

    View getView() {
        return (popup);
    }

    void show(boolean alignTop) {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

        if (alignTop) {
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            lp.setMargins(0, 20, 0, 0);
        } else {
            lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            lp.setMargins(0, 0, 0, 60);
        }

        hide();

        ((ViewGroup) map.getParent()).addView(popup, lp);
        isVisible = true;
    }

    void hide() {
        if (isVisible) {
            isVisible = false;
            ((ViewGroup) popup.getParent()).removeView(popup);
        }
    }
}
Foi útil?

Solução

Não tenho 100% de certeza se esse é o motivo, mas não vi ligação para Ver.setOnTouchListener no seu código para gestoListener.Eu teria esperado linha;

setOnTouchListener(gestureListener)

Depois de criá-lo.

Outras dicas

Tente retornar true de você MyGestureDetector.onDown.A implementação da superclasse deve retornar false que então suprime a entrega de eventos futuros até que o toque seja liberado.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top