Coder Social home page Coder Social logo

gx506578835 / swipebackfragment Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yokeyword/swipebackfragment

0.0 2.0 0.0 2.05 MB

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

License: Apache License 2.0

Java 100.00%

swipebackfragment's Introduction

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、支持Swipe时的滑动监听

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

Demo演示

如何使用

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

// appcompat v7包是必须的
compile 'me.yokeyword:swipebackfragment:0.2.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) {
                // 滑动百分比
            }
   });

    // 对于SwipeBackActivity有下面控制SwipeBack优先级的方法:
      /**
        * 限制SwipeBack的条件,默认栈内Fragment数 <= 1时 , 优先滑动退出Activity , 而不是Fragment
        *
        * 可以通过复写该方法, 自由控制优先级
        *
        * @return true: Activity可以滑动退出, 并且总是优先;  false: Activity不允许滑动退出
        */
        @Override
        public boolean swipeBackPriority() {
           return super.swipeBackPriority();
           // 下面是默认实现:
           // return getSupportFragmentManager().getBackStackEntryCount() <= 1;
        }

致谢

ikew0ng/SwipeBackLayout

swipebackfragment's People

Contributors

yokeyword avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.