SwipeBackLayout侧滑退出当前Activity与状态保存

最近想了解下,滑动退出是如何实现的,因此在网上看了一些例子,方式很多但是我觉得使用SwipeBackLayout是最简单的。

1.想要使用SwipeBackLayout需要在gradle中添加依赖:

    compile 'me.imid.swipebacklayout.lib:library:1.0.0'

2.新建一个BaseActivity继承SwipeBackActivity:

package jiaz.miki.com.myapplication;

import android.os.Bundle;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * Created by admin on 2018/4/13.
 * 滑动退出
 */
public class BaseActivity extends SwipeBackActivity{

    private SwipeBackLayout swipeBackLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        swipeBackLayout = getSwipeBackLayout();
        //设置滑动方向,可设置EDGE_LEFT(→), EDGE_RIGHT(←), EDGE_BOTTOM(↑),EDGE_ALL(←↑→) 
        swipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        swipeBackLayout.setEdgeSize(200);//滑动删除的效果只能从边界滑动才有效果,如果要扩大范围,可以调用这个方法
        setSwipeBackEnable(false);
    }
}

3.在使用的Activity中继承BaseActivity并在onCreate方法中使用getSwipeBackEnable(true/false);进行是否能够侧滑退出的控制,true为可以滑动退出,false为禁止。

4.以上功能已经实现,但是在滑动退出过程中会出现黑屏问题,解决方法:在styles.xml中添加:

true

即可解决。

5.可以通过控件对是否可以滑动进行控制同时可以保存状态:

package jiaz.miki.com.myapplication;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

/**
 * Created by admin on 2018/4/13.
 */

public class SecondActivity extends BaseActivity implements View.OnClickListener{

    private SharedPreferences preference;
    private SharedPreferences.Editor editor;
    private Button Swipe_on,Swipe_off;
    CheckBox isSwipe;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        preference = PreferenceManager.getDefaultSharedPreferences(this);
        isSwipe = (CheckBox) findViewById(R.id.isSwipe);
        boolean isswip = preference.getBoolean("remember_swipe",false);
        if(isswip){
            isSwipe.setChecked(true);
            isSwipe.isChecked();
            setSwipeBackEnable(true);
        }
        Swipe_on = (Button) findViewById(R.id.swipe_on);
        Swipe_off = (Button) findViewById(R.id.swipe_off);
        Swipe_on.setOnClickListener(this);
        Swipe_off.setOnClickListener(this);


        isSwipe.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                editor =preference.edit();
                if(isChecked){
                    setSwipeBackEnable(true);

                    editor.putBoolean("remember_swipe",true);
                }else{
                    setSwipeBackEnable(false);
                    editor.clear();
                }
                editor.apply();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.swipe_on:
                setSwipeBackEnable(true);
                break;
            case R.id.swipe_off:
                setSwipeBackEnable(false);
                break;
            default:
                setSwipeBackEnable(false);
                break;
        }
    }


}
6.但是我在学习这个方式进行滑动退出时,一直在思考如何实现滑动一定距离以上才能退出,否则无效的效果。目前还在研究汇总,成功后会更新。有会的大佬们请告知下!

7. 附demo下载路径: https://download.csdn.net/download/jia_zero/10351892


你可能感兴趣的:(SwipeBackLayout侧滑退出当前Activity与状态保存)