0

I wanted to imlement the swipe function over multiple textviews, where each view implements a onclick Listener. My problem is if i add onTouch to each textview and since they are small and many, they wont be able to recognize teh diference in both. I've tried to add to the parent layout the ontouch but it seems that since it has textviews it doesnt detect swipe over them.

Anyone know a good way to implement swipe left and right over the textviews and still preserve the onClick of these?

Thanks in advance.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Radaeld
  • 175
  • 1
  • 2
  • 9
  • you can detect touch over the whole area, record coordinate and translate them into detect textviews – ruben Mar 26 '18 at 19:53

1 Answers1

0

To give you an idea, I have a gridview (could be any Layout) where there are many cells, to detect swipe over all of these cells and also to detect click on each cell I have following code, Ofcourse you need to customize based on your need:

private GestureDetectorCompat gestureDetector;                                                               
protected void onAttachments() {
    gestureDetector = new GestureDetectorCompat(this.context, new SingleTapConfirm());
    //get the width and height of the grid view
    final int cellWidth = calendarGridView.getWidth() / 7;
    final int cellHeight = calendarGridView.getHeight() / 5;


    calendarGridView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent touchEvent) {

            int touchX = (int) touchEvent.getX();
            int touchY = (int) touchEvent.getY();

            if(touchEvent.getAction() == MotionEvent.ACTION_MOVE || gestureDetector.onTouchEvent(touchEvent)){

                if (touchX <= calendarGridView.getWidth() && touchY <= calendarGridView.getHeight()) {

                    int cellX = (touchX / cellWidth);
                    int cellY = (touchY / cellHeight);

                    touchPosition = cellX + (7 * (cellY));

                    positionPrinter.setText("child: " + cellX + "," + cellY);
                    cellIDprinter.setText(Integer.toString(touchPosition));

                    // Do you stuff
                    ... ... ... 
                }

            }

            return false;
        }
    });
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (gestureDetector != null)
        gestureDetector.onTouchEvent(event);

    return super.onTouchEvent(event);
}private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onSingleTapUp(MotionEvent event) {
        return true;
    }
}
ruben
  • 1,745
  • 5
  • 25
  • 47
  • and this still detects if initiate the swipe over the textview in your case over a cell or do i need to create the layout above the views? – Radaeld Mar 27 '18 at 00:53