Skip to content Skip to sidebar Skip to footer

Android Swipe To Change Fragments Not Working

I am trying to make an app where a user can swipe and change which fragment they are seeing on the screen. I can not use view pager because I want the user to be able to swipe to d

Solution 1:

First of all, you can really simplify your swipe code using droidQuery:

//global variablesprivatebooleanisSwiping=false;
private SwipeDetector.DirectionswipeDirection=null;
private View v;//set to the parent layout of the fragments.//swipe-handling code
$.with(v).swipe(newFunction() {
    @Overridepublicvoidinvoke($ droidQuery, Object... params) {
        if (params[0] == SwipeDetector.Direction.START)
            isSwiping = true;
        elseif (params[0] == SwipeDetector.Direction.STOP) {
            if (isSwiping) {
                isSwiping = false;
                if (swipeDirection != null) {
                    switch(swipeDirection) {
                        case DOWN :
                            //TODO: Down swipe complete, so do somethingbreak; 
                        case UP :
                            //TODO: Up swipe complete, so do somethingbreak; 
                        case LEFT :
                            //TODO: Left swipe complete, so do somethingbreak; 
                        case RIGHT :
                            //TODO: Right swipe complete, so do something (such as):
                            day++;
                            Fragment1rightFragment=newFragment1();
                            Bundleargs=newBundle();
                            args.putInt("day", day);
                            rightFragment.setArguments(args);

                            android.support.v4.app.FragmentTransactiontransaction= getSupportFragmentManager().beginTransaction();
                            transaction.replace(R.id.fragment_container, rightFragment);
                            transaction.addToBackStack(null);
                            transaction.commit();
                            break; 
                        default :
                            break; 
                    }
                }
            }
        }
        else {
            swipeDirection = (SwipeDetector.Direction) params[0];
        }
    }
});

You can find more on Fragment transactions here.

Also, consider keeping an int offset variable that keeps track of the +/- offset from zero. So for instance, you could get the already-instantiated Fragments from an ArrayList, then just swap out the one at mArrayList.get(offset), and when flinging right, do offset++, and 'offset--` for left swipes.

Edit

As requested in the comments, this code can be used to handle swipes and a child image click:

Include the SwipeInterceptorView in your main Layout (res/layout/main.xml):

<?xml version="1.0" encoding="utf-8"?><self.philbrown.SwipeInterceptorViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/swipe_view"android:layout_width="match_parent"android:layout_height="match_parent" ></self.philbrown.SwipeInterceptorView>

You will need to have class variables:

SwipeInterceptorView view;//instantiated in onCreate
ImageView fragImage;//must be instantiated when the new Fragment is transitioned in

Next, include the following components in onCreate:

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //set main view to the main layoutsetContentView(R.layout.main);
    //get a reference to the content view
    view = (SwipeInterceptorView) findViewById(R.id.swipe_view);
    //add Swiper
    view.setSwipeListener(newSwipeListener() {
        publicvoidonUpSwipe(View v) {
            //TODO handle up swipe
        }
        publicvoidonRightSwipe(View v) {
            //TODO handle right swipe
        }
        publicvoidonLeftSwipe(View v) {
            //TODO handle left swipe
        }
        publicvoidonDownSwipe(View v) {
            //TODO handle down swipe
        }
    });
    view.setOnTouchListener(newView.OnTouchListener() {
        @OverridepublicbooleanonTouch(View v, MotionEvent event) {
            returnsuper.onTouch(v, event);
        }
    });
}

When the new Fragment containing the ImageView is transitioned in, you need to reference it and update the swipe interceptor's onTouch method:

fragImage = (ImageView) fragment/* references the now non-null, on-display fragment */.getView().findViewById(R.id.yourImageId);
int[] origin = newint[2];
fragImage.getLocationOnScreen(origin);
final Rect bounds = new Rect(origin[0], origin[1], fragImage.getRight(), fragImage.getBottom());
view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    publicvoidonTouch(View v, MotionEvent event) {
        if (bounds.contains(event.getRawX(), event.getRawY())) {
            returnfalse;//now clicks will be handled by the Image.
        }
        return v.onTouchEvent(event);
    }
});

Solution 2:

Use ViewPager and FragmentPagerAdapter from Android Support Library v4.

Post a Comment for "Android Swipe To Change Fragments Not Working"