Swiping Over Multiple TextViews

161 Views Asked by At

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.

1

There are 1 best solutions below

1
On BEST ANSWER

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;
    }
}