Skip to content

jdsjlzx/SwipeBackFragment

 
 

Repository files navigation

SwipeBackFragment

An Android library that can finish a Fragment&Activity with swipe-back gesture.

滑动Fragment&Activity边缘即可类似IOS一样,拖动返回。

原理分析:传送门, 如果你重度使用Fragment,不妨看看这个库Fragmentation

特性

1、Activity内Fragment数大于1时,滑动返回的是Fragment,否则滑动返回的是Activity。

2、支持左、右、左&右滑动(未来可能会增加更多滑动区域)

3、支持Scroll中的滑动监听

4、帮你处理了Fragment重叠的情况

Demo演示

如何使用

1、项目下app的build.gradle中依赖:

// appcompat v7包是必须的
compile 'me.yokeyword:swipebackfragment:0.1.1'

2、如果Activity也需要支持SwipeBack,则继承SwipeBackActivity:

public class SwipeBackSampleActivity extends SwipeBackActivity {}

同时该Activity的theme添加如下属性:

 <item name="android:windowIsTranslucent">true</item>

3、如果Fragment需要支持SwipeBack,则继承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);
        // 需要支持SwipeBack则这里必须调用toSwipeBackFragment(view);
        return attachToSwipeBack(view);
    }
}

更多方法:

  // 设置滑动方向
  getSwipeBackLayout().setEdgeOrientation(SwipeBackLayout.EDGE_RIGHT); // EDGE_LEFT(默认),EDGE_ALL
  // 滑动过程监听
  getSwipeBackLayout().addSwipeListener(new SwipeBackLayout.OnSwipeListener() {
            @Override
            public void onDragStateChange(int state) {
                // Drag state
            }

            @Override
            public void onEdgeTouch(int edgeFlag) {
                // 触摸的边缘flag
            }

            @Override
            public void onDragScrolled(float scrollPercent) {
                // 滑动百分比
            }
   });

About

An Android library that can finish a Fragment&Activity with swipe-back gesture.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%