android 仿微信右滑返回
2017-06-02 16:52
393 查看
有这么一个开源项目,封装的右滑返回, 如果安卓基础不错的,可以 直接看源码: https://github.com/bingoogolapple/BGASwipeBackLayout-Android
以下是我单独分离出来一个仿微信右滑返回的小demo.
以下为效果图
![](https://img-blog.csdn.net/20170602162107497?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhbmdxdWZh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
下面是实现过程:
并且,在mainifest.xml里面更改默认的Application,改为 App
TextActivity.java
activity_test.xml
inc_toolbar.xml
5.这里要设置 Actiivty的样式
styles.xml
mainfest.xml 中的设置
这样就完成这个功能 了,如下是demo源码。
以下是我单独分离出来一个仿微信右滑返回的小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.javapublic 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" 4000 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源码。
相关文章推荐
- android 仿微信6.2右滑返回
- Android-通过SlidingPaneLayout高仿微信6.2最新版手势滑动返回(一)
- android 微信分享后显示白屏,需再点击返回才返回到app,该怎么解决啊,有人遇到过吗
- Android-通过SlidingPaneLayout高仿微信6.2最新版手势滑动返回(一)
- Android微信朋友圈向微信主界面返回在ActionBar中箭头按钮的实现
- Android精品源码,微信索引列表新闻客户端拖拽返回组件化方案页面框架
- Android按返回键程序仍在后台运行,并未完全退出和销毁,类似微信QQ等退出方式的实现-- moveTaskToBack
- Android调用微信原生分享接口,进入微信分享界面返回不了!
- android 微信分享,返回游戏, 游戏重启或闪退
- android 微信登录返回 -6
- android 解决微信登陆返回resp.errCode = -6
- Android-通过SlidingPaneLayout高仿微信6.2最新版手势滑动返回(一)
- Android应用实现点击back键时,使应用返回桌面,而不是退出应用,类似微信,qq等聊天软件
- Android仿微信右滑返回,简单实现
- Android精品源码,微信索引列表新闻客户端拖拽返回组件化方案页面框架
- Android使用SlidingPaneLayout 实现仿微信的滑动返回
- android接入微信登录点击授权登录后如何接收微信的返回数据
- Android仿微信右滑返回功能的实例代码
- Android-通过SlidingMenu高仿微信6.2最新版手势滑动返回(二)
- Android 高仿最新版微信 6.2 手势滑动返回(带阴影)