Pregunta

I'm facing a problem that I don't know how to solve and why all of this is happening.

It's simple. I've got a ImageView working as a button and when user click it something is moving on the screen and when the touch is realeased it stops.

So I've just set OnTouchListener on my ImageView and hoped it works but it doesn't.

I need to implement empty OnClickListener and then it's doing what it should do.

private void setSolveImage() {
    ImageView solve_button = (ImageView)getView().findViewById(R.id.solve_button);

    solve_button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {   
        }
    });

    solve_button.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                    screen.setTilesOrder(status.getCleanStatus()); // do something with screen
            } else if (action == MotionEvent.ACTION_UP) {
                    screen.setTilesOrder(status.getGameStatus()); // return screen to previous state
            }
        }
        return false;
        }
    });
}

Can you tell me please what am I doing wrong? Why do I have to do this this way?

¿Fue útil?

Solución

Just do one change and do a trick

do return true; instead return false; in setOnTouchListener()

No need to set setOnClickListener()

Otros consejos

return true if the callback consumed on touch, false otherwise. so change the return type as true instead of false in setOnTouchListener.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top