Skip to content Skip to sidebar Skip to footer

Swiping Over Multiple Textviews

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 s

Solution 1:

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

Post a Comment for "Swiping Over Multiple Textviews"