Today introduces a slide-out Fragment&Activity two-in-one component.

features

  1. If the number of fragments in an Activity is greater than 1, sliding returns the Fragment; otherwise, sliding returns the Activity.
  2. Support for left, right, left & right sliding (more sliding areas may be added in the future)
  3. Swipe listening is supported
  4. Help you deal with Fragment overlap

The demo presentation




swipe.gif

Method of use

  1. Build. Gradle dependencies in app under project:

    / / appcompat v7 package is a must compile 'me. Yokeyword: swipebackfragment: 0.2.1'Copy the code
  2. If your Activity also needs to support SwipeBack, then inherit SwipeBackActivity:

    public class SwipeBackSampleActivity extends SwipeBackActivity {}Copy the code

    Add the following attribute to the Activity’s theme:

    trueCopy the code
  3. If the Fragment needs to support SwipeBack, inherit the SwipeBackFragment:

    public class SwipeBackSampleFragment extends SwipeBackFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.xxx, container, false); // To support SwipeBack, you must call toSwipeBackFragment(view); return attachToSwipeBack(view); }}Copy the code
  4. More ways:

    // Set the sliding orientation getSwipeBackLayout().setedgeOrientation (swipebacklayout.edge_right); / / EDGE_LEFT (the default), EDGE_ALL / / sliding process to monitor getSwipeBackLayout () addSwipeListener (new SwipeBackLayout. OnSwipeListener () { @Override public void onDragStateChange(int state) { // Drag state } @Override public void onEdgeTouch(int edgeFlag) { Override public void onDragScrolled(float scrollPercent) {// float scrollPercent}}); // For SwipeBackActivity, there are the following methods to control the SwipeBack priority: /** Return true if the number of fragments in the stack is <= 1 and you want to exit the Activity by SwipeBack. Activities can slide out and always take precedence; */ @override public Boolean swipeBackPriority() {return super.swipebackPriority (); / / the following is the default implementation: / / return getSupportFragmentManager () getBackStackEntryCount () < = 1; }Copy the code

    Thanks ikew0ng/SwipeBackLayout