Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine which VIEW in multiple views has gesture (double click, swipe left, swipe right, etc)?

I have a scrollview with multiple relativelayout views horizontally... one for each record in a database... that are created programmatically.

I need to determine which view is affected by some gestures... click, doubleclick, and left/right swipe.

Of course the CLICK I was able to get with:

     RelativeLayout rlView = new RelativeLayout(this);
     rlView.setId(10000+myrecordid);
     rlView.setOnClickListener(myviewclick);

and the myviewclick:

private View.OnClickListener myviewclick = new View.OnClickListener() {
    public void onClick(View v) {
        Integer i=v.getId()-10000;
        // Do some processing on this view
    }
};

From what I found online, I tried to get the gesture this way:

rlView.setOnTouchListener(myviewtouch);

with this code:

private View.OnTouchListener myviewtouch = new View.OnTouchListener(){
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.i("MYLOG","double tap");
            return true;
        }
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.i("MYLOG","SingleTapConfirmed");
            return true;
        }
        @Override
        public void onLongPress(MotionEvent e) {
            Log.i("MYLOG","LongPress");
        }

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (e1.getX()<e2.getX())
                Log.i("MYLOG","Fling Right");
            else
                Log.i("MYLOG","Fling Left");
            return true;
        }
    });
};

According to MYLOG, I am getting the appropriate gestures as needed. The problem is, I don't know how to get the views ID that the gesture was in. I know it is in the onTouch but that called the gestureDetector.OnTouchEvent to determine the motion... and I am lost at this point.

I searched all over StackOverflow and other sites for several hours... they all show variances on how to determine the gesture... but having trouble finding anything about using with multiple views that I can use.

Any help would be appreciated.

like image 900
Peter Avatar asked Dec 07 '15 02:12

Peter


1 Answers

I think I figured it out. Probably not the most elegant way to do it, and there might be other "issues" because of handling it this way, but for now it works. But if others find this problem, here's how I worked around it:

Here is how my modified code worked out. I added a public variable called vTouch:

private View.OnTouchListener myviewtouch = new View.OnTouchListener(){
    public View vTouch;
    public boolean onTouch(View v, MotionEvent event) {
        vTouch=v;
        return gestureDetector.onTouchEvent(event);
    }

    GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Integer viewId=vTouch.getId();
            Log.i("MYLOG","double tap in view: "+viewId);
            return true;
        }
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Integer viewId=vTouch.getId();
            Log.i("MYLOG","SingleTapConfirmed in view: "+viewId);
            return true;
        }
        @Override
        public void onLongPress(MotionEvent e) {
            Integer viewId=vTouch.getId();
            Log.i("MYLOG","LongPress in view: "+viewId);
        }

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Integer viewId=vTouch.getId();
            if (e1.getX()<e2.getX())
                Log.i("MYLOG","Fling Right in view: "+viewId);
            else
                Log.i("MYLOG","Fling Left in view: "+viewId);
            return true;
        }
    });
};

By using the internal public variable vTouch I can now reference the view within each gesture event where ever the "view" is needed that the gesture happened to be in.

like image 51
Peter Avatar answered Nov 06 '22 11:11

Peter