zoukankan      html  css  js  c++  java
  • 用SwipeBackLayout实现滑动关闭当前Activity

      

      说起SwipeBackLayout,我对它还是有一定怨念的。当时就希望能实现关闭当前Activity的效果,但完全搜不当相关的东西,最后好不容易搜到了这个SwipeBackLayout,觉得可以实现滑动关闭了,但用上后却出现了黑屏的问题,好在最后都解决了。这也说明了任何一个开源项目都是在不断完善的,完善的动力就是靠大家的提意见和热情,SwipeBackLayout作为中国的一款优秀开源项目值得让大家称赞!

    项目地址:https://github.com/qmdx/SwipeBackLayout

    一、让需要滑动的Activity基础自定义的style

    这里就是为了解决滑动黑屏的问题

        <style name="KaleTheme" parent="AppBaseTheme">
            <!-- 解决activity切换时的黑屏问题 -->
            <item name="android:windowIsTranslucent">true</item>  
        </style>

    styles.xml中的全部文件:

    <resources>
    
        <!--
            Base application theme, dependent on API level. This theme is replaced
            by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
        -->
        <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
            <!--
                Theme customizations available in newer API levels can go in
                res/values-vXX/styles.xml, while customizations related to
                backward-compatibility can go here.
            -->
        </style>
    
        <!-- Application theme. -->
        <style name="AppTheme" parent="AppBaseTheme">
            <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        </style>
        
        <style name="KaleTheme" parent="AppBaseTheme">
            <!-- 解决activity切换时的黑屏问题 -->
            <item name="android:windowIsTranslucent">true</item>  
        </style>
    
    </resources>

    我是直接用Application使用了这个样式,仅仅为了演示。

     <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/KaleTheme" >

    二、用Activity继承一个类

    如果你是要兼容Actionbar那么就继承SwipeBackActionbarActivity,这个类是我自己改的,原来的lib中没有。如果不用兼容,那么直接用SwipeBackActivity即可。

    三、在方法中找到SwipeBackLayout,并设置滑动的区域和方向

    这个就是简单的设置,我直接贴代码了。

    package com.kale.swipbacklayouttest;
    
    import me.imid.swipebacklayout.lib.SwipeBackLayout;
    import me.imid.swipebacklayout.lib.app.SwipeBackActionbarActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    /**
     * @author:Jack Tony
     * @tips  :如果要兼容,那么继承SwipeBackActionbarActivity,否则继承SwipeBackActivity
     * @date  :2014-10-31
     */
    public class MainActivity extends SwipeBackActionbarActivity {
    
         private SwipeBackLayout mSwipeBackLayout;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            mSwipeBackLayout = getSwipeBackLayout();
            //设置可以滑动的区域,推荐用屏幕像素的一半来指定
            mSwipeBackLayout.setEdgeSize(200);
            //设定滑动关闭的方向,SwipeBackLayout.EDGE_ALL表示向下、左、右滑动均可。EDGE_LEFT,EDGE_RIGHT,EDGE_BOTTOM
            mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_ALL);
            
            Button btn = (Button)findViewById(R.id.open_button);
            btn.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO 自动生成的方法存根
                    startActivity(new Intent(MainActivity.this,MainActivity.class));
                }
            });
        }
    
    
    }

    上面还添加了一个button,是用来开启新的Activity,主要是便于测试的。好啦,你看使用方式十分简单吧,下面是源码:

    源码下载:http://download.csdn.net/detail/shark0017/8104885

  • 相关阅读:
    移动端
    vue2.0实战记录
    vue学习记录
    JS中数字计算精度
    10亿个字符串的排序问题
    <nginx.conf> nginx用户权限
    详解jquery插件中;(function ( $, window, document, undefined )的作用
    nginx服务器安装及配置文件详解
    移动端HTML5<video>视频播放优化实践
    打造自己的html5视频播放器
  • 原文地址:https://www.cnblogs.com/tianzhijiexian/p/4065069.html
Copyright © 2011-2022 走看看