sockeqwe/SwipeBack

How to use it in fragment?

kyze8439690 opened this issue · 4 comments

Great library, I think this library is better than https://github.com/Issacw0ng/SwipeBackLayout because it has an indicator and lots of custom options, but how can I use this library in fragment?For example, in onCreateView, how can I get the swipeback view?

Hi, thanks,
the SwipeBack will be attached to the Activity and not to a Fragment.

So initialize the SwipeBack in the Activity.
If you really need have to access the SwipeBack object from a fragment you can use fragment.getActivity() to access the Activity where the SwipeBack is attached to. Your activity needs to provide a method to access it.

Something like this:

public class MyFragment extends Fragement{


   public void onCreateView(...){

      MyActivity activity (MyActivity) getActivity();
      SwipeBack swipeBack  = activity.getSwipeBack(); // Implement such a method in your activity

      ...
   }

}

@sockeqwe thanks for you awesome library, which exactly what I am looking for. I have implemented the swipe back feature via a ViewPagerActivity just as your examples show, and it works really cool.

But I was confused that where to find the swipe back callback when a swiping gesture was done successfully.

For instance, I have a ViewPagerActivity which has two Fragments, e.g. FragmentA and FragmentB. I have configured the FragmentA to support swipe back feature, and FragmentA show a directory structure.

My problem is, when clicked into a sub directory in FragmentA, then swipe back, unfortunately it finished the ViewPagerActivity instead of navigate back to the parent directory in FragmentA. So I wonder how to handle the swipe back callback. I have tried to implement the nav back function in onBackPressed, turned out it doesnt work for me. Can you provide me some clue, I cant find any examples in the sample project as well.

You have to provide your own callback implementation:

SwipeBack.attach(this, Position.LEFT)
        .setSwipeBackTransformer(new MyCallbackTransformer());

Have a look here:
https://github.com/sockeqwe/SwipeBack/tree/master/library/src/com/hannesdorfmann/swipeback/transformer

Thanks for such a fast responding, now I know how to implement it.