3

I need to swipe horizontally between list of questions and their respective choices in a radio group. I have read that I have different choices such as: 1- Gesture Detector 2- ViewPage 3- ViewPage with fragments

I have tried ViewPage but I am facing different problems on how to swipe backward and indicating which choice has been checked by the user. I need to use the easiest way to swipe forward and backward (indicating which choice has been checked)

I want to depend on Andriod methods as much as possible without storing the checked choices for example by myself since I believe that it is more optimized such methods from both run-time or code size. Please if there are other classes that I can use, guide me on that or which of them is really the best to perform my target

Wael Showair
  • 3,092
  • 2
  • 22
  • 29

3 Answers3

1

I would use ViewPager with a FragmentPagerAdapter. The items of the adapter would be different instances of the same fragment. Each fragment would contain a static question text and a radio group of possible answers to the question. ViewPager then will take care of swiping substituting one such fragment with another.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
0

I highly recommend this Library:

ViewPagerIndicator by Jake Warton

Is very customizable.

Chronos
  • 1,972
  • 17
  • 22
0

You can easilly use the gesture detector creating a class that extend SimpleOnGestureListener that possess the onFling methods like that :

protected class ExampleGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {// User swipe vertically
        }
        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_MIN_VELOCITY) {// Right swipe
        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_MIN_VELOCITY) {// Left swipe
        }
        return false;
    }

}

You just have to define the variable as you want, create your gestureDetector like

gestureDetector = new GestureDetector(new ExampleGestureDetector());

And redefine the touch listener for the view you want

view.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View wv, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        });

EDIT : After testing it, it appear that it only work with scrollable item.