android 仿微信右滑返回
来源:互联网 发布:淘宝什么是千人千面 编辑:程序博客网 时间:2024/05/19 12:26
有这么一个开源项目,封装的右滑返回, 如果安卓基础不错的,可以 直接看源码: https://github.com/bingoogolapple/BGASwipeBackLayout-Android
以下是我单独分离出来一个仿微信右滑返回的小demo.
以下为效果图
下面是实现过程:
1.首页要依赖如下包:
compile 'cn.bingoogolapple:bga-swipebacklayout:1.0.8@aar'
2. 新建 App 用来初始化swipebacklayout控件
public class App extends Application { @Override public void onCreate() { super.onCreate(); // 必须在 Application 的 onCreate 方法中执行 BGASwipeBackManager.getInstance().init(this) 来初始化滑动返回 BGASwipeBackManager.getInstance().init(this); }}
并且,在mainifest.xml里面更改默认的Application,改为 App
3.封装一个BaseActivity 让所有有右滑返回功能的Activity 继承
public abstract class BaseActivity extends AppCompatActivity implements BGASwipeBackHelper.Delegate, View.OnClickListener { protected BGASwipeBackHelper mSwipeBackHelper; protected Toolbar mToolbar; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { // 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackManager.getInstance().init(this) 来初始化滑动返回」 // 在 super.onCreate(savedInstanceState) 之前调用该方法 initSwipeBackFinish(); super.onCreate(savedInstanceState); initView(savedInstanceState); mToolbar = getViewById(R.id.toolbar); setListener(); processLogic(savedInstanceState); } /** * 初始化滑动返回。在 super.onCreate(savedInstanceState) 之前调用该方法 */ private void initSwipeBackFinish() { mSwipeBackHelper = new BGASwipeBackHelper(this, this); // 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackManager.getInstance().init(this) 来初始化滑动返回」 // 下面几项可以不配置,这里只是为了讲述接口用法。 // 设置滑动返回是否可用。默认值为 true mSwipeBackHelper.setSwipeBackEnable(true); // 设置是否仅仅跟踪左侧边缘的滑动返回。默认值为 true mSwipeBackHelper.setIsOnlyTrackingLeftEdge(true); // 设置是否是微信滑动返回样式。默认值为 true mSwipeBackHelper.setIsWeChatStyle(true); // 设置阴影资源 id。默认值为 R.drawable.bga_sbl_shadow mSwipeBackHelper.setShadowResId(R.drawable.bga_sbl_shadow); // 设置是否显示滑动返回的阴影效果。默认值为 true mSwipeBackHelper.setIsNeedShowShadow(true); // 设置阴影区域的透明度是否根据滑动的距离渐变。默认值为 true mSwipeBackHelper.setIsShadowAlphaGradient(true); // 设置触发释放后自动滑动返回的阈值,默认值为 0.3f mSwipeBackHelper.setSwipeBackThreshold(0.3f); } /** * 是否支持滑动返回。这里在父类中默认返回 true 来支持滑动返回,如果某个界面不想支持滑动返回则重写该方法返回 false 即可 * * @return */ @Override public boolean isSupportSwipeBack() { return true; } /** * 正在滑动返回 * * @param slideOffset 从 0 到 1 */ @Override public void onSwipeBackLayoutSlide(float slideOffset) { } /** * 没达到滑动返回的阈值,取消滑动返回动作,回到默认状态 */ @Override public void onSwipeBackLayoutCancel() { } /** * 滑动返回执行完毕,销毁当前 Activity */ @Override public void onSwipeBackLayoutExecuted() { mSwipeBackHelper.swipeBackward(); } @Override public void onBackPressed() { // 正在滑动返回的时候取消返回按钮事件 if (mSwipeBackHelper.isSliding()) { return; } mSwipeBackHelper.backward(); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { onBackPressed(); return true; } return super.onOptionsItemSelected(item); } /** * 初始化布局以及View控件 */ protected abstract void initView(Bundle savedInstanceState); /** * 给View控件添加事件监听器 */ protected abstract void setListener(); /** * 处理业务逻辑,状态恢复等操作 * * @param savedInstanceState */ protected abstract void processLogic(Bundle savedInstanceState); /** * 需要处理点击事件时,重写该方法 * * @param v */ public void onClick(View v) { } /** * 查找View * * @param id 控件的id * @param <VT> View类型 * @return */ protected <VT extends View> VT getViewById(@IdRes int id) { return (VT) findViewById(id); } @Override protected void onStop() { super.onStop(); Log.i(this.getClass().getSimpleName(), "onStop " + this.getClass().getSimpleName()); }}
4. 使用两个测试Activity
MainActiivty.java
public class MainActivity extends BaseActivity { @Override protected void initView(Bundle savedInstanceState) { setContentView(R.layout.activity_main); } @Override protected void setListener() { } @Override protected void processLogic(Bundle savedInstanceState) { initToolbar(); } private void initToolbar() { setSupportActionBar(mToolbar); getSupportActionBar().setTitle("测试滑动删除 MainActivity"); TextView tv = getViewById(R.id.tv); tv.setText("MainActivity"); Button btn = getViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mSwipeBackHelper.forward(TestActivity.class); } }); }}
TextActivity.java
public class TestActivity extends BaseActivity { @Override protected void initView(Bundle savedInstanceState) { setContentView(R.layout.activity_test); } @Override protected void setListener() { } @Override protected void processLogic(Bundle savedInstanceState) { initToolbar(); } private void initToolbar() { setSupportActionBar(mToolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle("测试滑动删除 TestActivity"); TextView tv = getViewById(R.id.tv); tv.setText("TestActivity"); }}
两个布局文件:(这里要给根布局设置背景颜色,不然增加样式后activity会为透明)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:fitsSystemWindows="true" android:orientation="vertical" tools:context="com.bxn.swipedemo.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:elevation="0dp"> <include layout="@layout/inc_toolbar" /> </android.support.design.widget.AppBarLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textColor="@color/colorAccent" /> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="btn" /> </LinearLayout></android.support.design.widget.CoordinatorLayout>
activity_test.xml
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:fitsSystemWindows="true" android:orientation="vertical" tools:context="com.bxn.swipedemo.TestActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:elevation="0dp"> <include layout="@layout/inc_toolbar" /> </android.support.design.widget.AppBarLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <TextView android:id="@+id/tv" android:textColor="@android:color/black" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="btn" /> </LinearLayout></android.support.design.widget.CoordinatorLayout>
inc_toolbar.xml
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" style="@style/Toolbar"/>
5.这里要设置 Actiivty的样式
styles.xml
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="Toolbar"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">?attr/actionBarSize</item> <item name="android:background">?attr/colorPrimary</item> <item name="android:theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item> <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item> </style> <!-- 适用于开启滑动返回功能的 Activity --> <style name="AppTheme.Transparent"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsTranslucent">true</item> </style></resources>
mainfest.xml 中的设置
<activity android:name=".MainActivity" android:theme="@style/AppTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".TestActivity" android:theme="@style/AppTheme.Transparent" android:windowSoftInputMode="stateHidden|adjustResize" />
这样就完成这个功能 了,如下是demo源码。
阅读全文
0 0
- android 仿微信右滑返回
- Android仿微信右滑返回,简单实现
- android屏蔽返回键
- Android 捕获Activity 返回
- android 自定义返回按钮
- android 笔记--返回homescreen
- Android 返回return处理
- android 返回键 退出
- android 返回HOME界面
- android返回HOME界面
- android屏蔽返回键
- Android 返回键事件
- Android自定义返回按钮
- Android返回键推出
- android返回二次退出
- Android返回HOME界面
- android 返回键退出
- Android 返回键
- Activiti系列(一)——最简单的Activiti程序
- 光电隔离RS485典型电路
- background-attachment: fixed;
- 设置C++崩溃时生成Dump文件
- Divide_Two_Integers(低时间复杂度版)
- android 仿微信右滑返回
- Python 判断密码等级的强弱,不使用正则表达式
- ASP.NET-Request对象
- 解决在thinkPhP中加载css和js的问题
- 卷积神经网络CNN
- XML基本语法
- C#网络编程(一.2、Socket)
- nio tcp之Socket系列通道 (四)
- 28 WebGL绘制立方体