Endi Tóth February 2016

Using GestureDetector and clickable RelativeLayout

I have a problem with using GestureDetector and clickable RelativeLayout at the same time. When my RelativeLayout is not clickable then the app recognize every gesture perfectly, but when I set the layout to clickable then the app doesn't recoginze the gestures. Is there any way to use both at the same time?

Answers


Swas_99 February 2016

Unfortunately, onClick() & onTouch() cannot behave normally when used together. I use a work-around to detect a click using onTouch. I am not sure how it will behave with gesture detector present. I will share my code.. It works fine without gesture detector. Let me know if it's not working.

Initializing your listeners :

GestureDetector gdt;
View.OnTouchListener touchListener;
private void initializeListener()
{
    gdt =  new GestureDetector(mContext,new GestureListener());
    touchListener = new View.OnTouchListener() {
        float startX;
        float startY;
        float CLICK_ACTION_THRESHOLD = 5;
        //Uses the touched coordinates recorded through MOTION_DOWN & MOTION_UP to detect whether the action performed was a click
        private boolean isAClick(float startX, float endX, float startY, float endY) {
            if(isLoadingView)
                return false;

            float differenceX = Math.abs(startX - endX);
            float differenceY = Math.abs(startY - endY);
            return !(differenceX > CLICK_ACTION_THRESHOLD || differenceY > CLICK_ACTION_THRESHOLD);
        }
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startX = event.getX();
                    startY = event.getY();
                    break;
                case MotionEvent.ACTION_UP: {
                    float endX = event.getX();
                    float endY = event.getY();

                    if (isAClick(startX, endX, startY, endY))
                    {
                        switch (v.getId())
                        {
                            case R.id.view_id:
                            break

                        }
                    }
                }
                break;
            }
            gdt.onTouchEvent(event);
            return true;
        }
    };
}

              

Post Status

Asked in February 2016
Viewed 2,545 times
Voted 7
Answered 1 times

Search




Leave an answer