10 Star 18 Fork 2

欧阳锋 / Android_Slide_To_Close

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Snake

温馨提示:Snake已经完成AndroidX适配,请放心使用!

旧版本文档

最新版本

模块 snake-androidx snake-compiler-androidx
最新版本 Download Download

特性介绍

  • 同时支持Activity,Fragment
  • 使用简单,一行代码接入,无侵入性
  • 配置灵活,可以适配各种复杂场景
  • 适配SDK Version >= 14以上所有机型,无副作用

Demo下载体验

扫描图中二维码下载

扫描上方二维码 或 直接点这里下载

使用方法

1)添加依赖

dependencies {
    // x.x.x代表上方表格中对应模块最新版本
    implementation 'com.youngfeng.android:snake-androidx:x.x.x'
    annotationProcessor 'com.youngfeng.android:snake-compiler-androidx:x.x.x'
}

注:如果使用Kotlin,请将annotationProcessor修改为kapt

2)在Application中对初始化Snake

public class SnakeApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        
        // 对Snake进行初始化
        Snake.init(this);
    }
}

Activity集成步骤

在需要开启滑动返回的Activity类上方添加注解@EnableDragToClose即可。

@EnableDragToClose()
public class FirstActivity extends Activity

Fragment集成步骤

在Fragment中使用Snake实现滑动关闭比Activity更加灵活,推荐大家使用Fragment进行页面布局。目前,Snake提供了两种方式在Fragment中开启滑动关闭。

方法一:动态配置

第一步:在需要开启滑动返回的Fragment类上方添加注解@EnableDragToClose

@EnableDragToClose(
public class FirstFragment extends Fragment {

第二步:使用Snake提供的方法动态创建Fragment实例。

// 如果Fragment继承自androidx.fragment.app.Fragment, 则使用方法newProxySupport创建实例
FirstFragment fragment = Snake.newProxySupport(FirstFragment.class);

// 如果Fragment继承自android.app.Fragment, 则使用个方法newProxy创建实例
FirstFragment fragment = Snake.newProxy(FirstFragment.class);

注意:在不存在默认构造函数的情况下,你需要使用注解@PrimaryConstructor指定将要用于创建Fragment实例的构造方法。

@EnableDragToClose()
public class FirstFragment extends Fragment {
    
    @PrimaryConstructor
    public FirstFragment(int x, int y) {
        
    }
    
    ...
}

在使用了注解标记构造函数的情况下,使用Snake方法创建实例的时候需要传入构造参数,如下所示:

FirstFragment fragment = Snake.newProxySupport(FirstFragment.class, 1, 2);

方法二:使用继承方式集成

按照下面的对应关系,改变你的Fragment父类即可完成滑动关闭集成:

  • android.app.Fragment => com.youngfeng.snake.app.Fragment
  • androidx.fragment.app.Fragment => com.youngfeng.snake.androidx.app

注意

1)从0.4.0版本开始,support库将不再提供支持,如需继续使用support库版本Fragment,请使用0.4.0以下版本,Activity不受影响。

2)由于Android 9.0已经舍弃了android.app.Fragment类,Snake也将不再提供对该类支持,推荐大家使用androidx.app.Fragment代替。

3)Snake与Navigation暂时不兼容,如需使用Snake实现滑动关闭,需要自己控制Fragment页面导航。

滑动参数配置

通常情况下,完成上面的步骤,你已经可以正常使用滑动关闭功能了。可是,有些同学可能希望对滑动样式进行定制化。别担心, Snake提供了两种方式对滑动参数进行配置。

  • 全局滑动参数配置 如果你希望对所有页面应用滑动参数配置,可以使用snake.xml文件对参数进行配置,在工程的根目录下面,我提供了配置模板
<?xml version="1.0" encoding="utf-8"?>
<snake>
    <config>
        <!-- 设置为true,根Activity也能够滑动关闭,这很奇怪!不建议修改这个变量的默认值 -->
        <enable_for_root_activity>false</enable_for_root_activity>
        <!-- 设置为true,将监听当前页面所有位置往右快速滑动手势 -->
        <only_listen_to_fast_swipe>false</only_listen_to_fast_swipe>
        <!-- 快速滑动最低检测速度,不建议修改。过高会影响灵敏度,过低会导致误判 -->
        <min_velocity>2000</min_velocity>
        <!-- 设置为true,滑动时左侧边缘阴影将被隐藏, 这个变量的默认值也不建议修改 -->
        <hide_shadow_of_edge>false</hide_shadow_of_edge>
        <!-- 阴影边缘渐变色起始颜色 -->
        <shadow_start_color>#00000000</shadow_start_color>
        <!-- 阴影边缘渐变色结束颜色 -->
        <shadow_end_color>#50000000</shadow_end_color>
        <!-- 类似iPhone X, 从底部边缘快速上滑回到桌面 (实验性功能,默认关闭) -->
        <enable_swipe_up_to_home>false</enable_swipe_up_to_home>
        <!-- 是否允许页面联动,默认为true -->
        <allow_page_linkage>true</allow_page_linkage>
    </config>
</snake>

修改模板参数,复制当前xml文件,放到主工程目录的assets下面即可,名称必须依然是snake.xml,不能修改!

  • 单页面参数配置 如果你只希望对单个页面应用滑动参数配置,可以使用**@SetDragParameter**对其进行配置:
@EnableDragToClose()
@SetDragParameter(minVelocity = 2000, hideShadowOfEdge = false ...)
public class FirstActivity extends Activity

其它接口介绍

Snake.enableDragToClose():如果你希望动态开启或关闭【滑动关闭】特性,可以使用该接口

Snake.addDragListener():如果你希望在滑动过程中进行一些额外的处理,可以使用该接口监听整个滑动过程。

Snake.setCustomTouchInterceptor:如果你在使用过程中,出现了一些滑动冲突问题,你可以通过使用该接口自定义拦截器解决。 注意:大多数情况下你不需要理会该接口,如果确定是需要解决这种滑动冲突问题,可以使用该接口。

Snake.dragToCloseEnabled():如果你需要知道滑动关闭功能在当前页面是否处于开启状态,可以使用该接口。

Snake.enableSwipeToHome(): 如果希望在某个页面开启上滑退出到桌面功能,可以使用该接口

Snake.swipeUpToHomeEnabled(): 获取当前页面上滑退出到桌面功能开启状态

版本兼容问题处理

support替换为androidx实现

由于0.4.0版本已经移除了support库,如果你在Fragment中使用了Snake,请将support库替换为androidx实现。

移除SnakeAnimationController接口

0.4.0版本开始,snake-compiler将自动实现该接口,无需再自行实现该接口。

android.app.Fragment替换为androidx实现

0.4.0版本开始,将逐步放弃对系统Fragment的支持,如果你在Fragment中使用了系统实现,请替换为androidx实现。

依赖声明方式发生变化,需要增加-androidx后缀

implementation 'com.youngfeng.android:snake-androidx:x.x.x'
annotationProcessor 'com.youngfeng.android:snake-compiler-androidx:x.x.x'

最佳实践

1)为了避免出现大量重复代码,推荐大家使用继承的方式使用Snake。

2)Activity的启动是一个非常耗时的过程,为了体验效果更佳,推荐使用全Fragment设计,或者单Activity+多Fragment设计。

3)遇到问题请先查看Wiki,如果没有你想要的答案,请添加QQ交流群288177681及时反馈。

4)为了在Activity中获得最佳使用体验,建议大家在style文件中添加如下配置:

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

5)android.app.Fragmentandroid.support.v4.app.Fragment都已经被Android官方舍弃,推荐大家始终使用androidx.fragment.app.Fragment作为Fragment类唯一选择。

混淆配置

# 如果已经应用该规则,无需重复配置
-keepattributes *Annotation*
-keep class **.*_SnakeProxy
-keep @com.youngfeng.snake.annotations.EnableDragToClose public class *

微信公众号

欧阳锋工作室

微信公众号也是一个高效的问题反馈平台,如需帮助请在微信公众号中给我留言,我会第一时间查看!*

QQ交流群

QQ群:288177681

如果你在使用过程中遇到了任何问题,欢迎加群交流。如果你想给作者支持,请点击上方star支持。

PS: 如果你在产品中使用了Snake, 请来信告诉我!邮件地址:626306805@qq.com,非常感谢!

License

Copyright 2018 Ouyangfeng Office

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2018 Ouyangfeng Office Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

使用Snake框架,Android也可以轻松实现边缘侧滑关闭功能,效果接近iOS 展开 收起
Android 等 2 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Android
1
https://gitee.com/ouyangfeng/Android_Slide_To_Close.git
git@gitee.com:ouyangfeng/Android_Slide_To_Close.git
ouyangfeng
Android_Slide_To_Close
Android_Slide_To_Close
master

搜索帮助