前言:

用最简单的例子来说明此问题。

1.在Activity中加上默认的布局Layout

2.在自定义的Layout中实现右滑关闭Activity的逻辑

直接上代码!

自定义的布局HFFinishRelativeLayout!

package com.huofar.widget;import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.RelativeLayout;import com.huofar.util.LogUtil;/*** Created by zhangxiwei on 14/11/25.*/
public class HFFinishRelativeLayout extends RelativeLayout{private static final String TAG = LogUtil.makeLogTag(HFFinishRelativeLayout.class);public interface ScrollLeftFinishListener{public void finishPage();}FragmentActivity activity;private ScrollLeftFinishListener scrollLeftFinishListener;public void setScrollLeftFinishListener(ScrollLeftFinishListener scrollLeftFinishListener) {this.scrollLeftFinishListener = scrollLeftFinishListener;}// 滑动距离及坐标private float xDistance, yDistance, xLast, yLast;public HFFinishRelativeLayout(Context context) {super(context);}public HFFinishRelativeLayout(Context context, AttributeSet attrs) {super(context, attrs);}public void attachToActivity(FragmentActivity activity) {this.activity = activity;TypedArray a = activity.getTheme().obtainStyledAttributes(new int[] { android.R.attr.windowBackground });int background = a.getResourceId(0, 0);a.recycle();ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);decorChild.setBackgroundResource(background);decor.removeView(decorChild);addView(decorChild);decor.addView(this);}@Overridepublic boolean onInterceptTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:break;case MotionEvent.ACTION_MOVE:}return super.onInterceptTouchEvent(event);}
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:xDistance = yDistance = 0f;xLast = event.getX();yLast = event.getY();break;case MotionEvent.ACTION_MOVE:final float curX = event.getX();final float curY = event.getY();xDistance += Math.abs(curX - xLast);yDistance += Math.abs(curY - yLast);if (curX > xLast && xDistance > yDistance && xDistance > 300) {if(scrollLeftFinishListener != null){xLast = curX;yLast = curY;scrollLeftFinishListener.finishPage();return true;}}xLast = curX;yLast = curY;}return super.dispatchTouchEvent(event);}@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:break;case MotionEvent.ACTION_MOVE:break;}<strong><span style="color:#ff6666;">return true;</span></strong>}
}

重点观看上面的红色加粗字段。

我处理的是每次滑动向右滑动300px执行关闭操作,在需要的使用的Activity实现借口直接finish就OK了!

Activity中的调用:

package com.huofar.activity;import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;import com.huofar.R;
import com.huofar.widget.HFFinishRelativeLayout;/*** Created by zhangxiwei on 14/11/25.*/
public class HFBaseActivity extends FragmentActivity implements HFFinishRelativeLayout.ScrollLeftFinishListener  {private boolean isFinishScrollLeft;HFFinishRelativeLayout hfFinishRelativeLayout;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);isFinishScrollLeft = true;//延后为了等待需要屏蔽返回滑动接受参数new Handler().postAtTime(new Runnable() {@Overridepublic void run() {setFinishScrollLeft(isFinishScrollLeft);}},1000);}public void setFinishScrollLeft(boolean isFinishScrollLeft) {this.isFinishScrollLeft = isFinishScrollLeft;if(isFinishScrollLeft){if(isFinishScrollLeft) {hfFinishRelativeLayout = (HFFinishRelativeLayout) LayoutInflater.from(this).inflate(R.layout.activity_finish_base, null);hfFinishRelativeLayout.attachToActivity(this);hfFinishRelativeLayout.setScrollLeftFinishListener(this);}}}@Overridepublic void finishPage() {finish();}
}

所有的代码就是这点,延迟一秒的就是为了接受isFinishScrollLeft变量,我是在已有的工程中修改的,就是为了在有些页面不需要关闭传此变量就OK了!。方法有点笨。大家有好方法可以提供。先多谢!

接下来就是头疼的问题,为什么要这么做:

恶补知识:1.dispatchTouchEvent 2.onInterceptTouchEvent 3.onTouchEvent 传递事件  直接给你们来个学习地址

http://www.cnblogs.com/sunzn/archive/2013/05/10/3064129.html

除此之外看的迷迷糊糊的可以自己写一个demo实践一下 或者多google几个介绍看看,就是事件的传递,然后在dispatchTouchEvent接受一下。然后直接关闭就行。

其实也没有什么,帮助想要该功能的童鞋。

有什么问题可以跟帖询问。

补上activity_finish_base.xml

<?xml version="1.0" encoding="utf-8"?>
<com...widget.HFFinishRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"></com....widget.HFFinishRelativeLayout>

Android 向右滑动关闭页面相关推荐

  1. android防止左向右滑出程序,Android向右滑动关闭Activity(高仿知乎微信)

    先上一个效果图,不是很清晰,凑合看下. 大概效果就是,Activity向右滑动,滑动超过屏幕的一半,就关闭,否则,恢复原来的状态. 下面让我们来看下实现原理吧. 1.配置透明主题 要想Activity ...

  2. 安卓.点击头像--编辑个人姓名--提交后.同时调用js关闭页面--返回上一层

    安卓.点击头像-->编辑个人姓名-->提交后.同时调用js关闭页面-->返回上一层 $(document).ready(function() { $('#selfbtn').clic ...

  3. 调试Android和iOS页面

    1.Chrome DevTools远程调试Android和iOS页面 电脑上安装新版谷歌浏览器,然后地址栏输入chrome://inspect会打开设备监视页面,这里可以监视到iOS和Android设 ...

  4. android切换页面上滑动动画,Android ViewPager多页面滑动切换以及动画效果

    评论 #28楼[楼主] 2012-06-01 14:27D.Winter @孤寒江雪 我猜 要么在头尾各再加入一个页卡 在页卡切换监听中判断,如果选中了头尾的页卡,就返回到相邻的那个页卡.头尾页卡的界 ...

  5. 关闭页面时执行“退出”的解决方案

    在有些应用中我们需要实时的更新站点用户是否在线的状态.比如一些论坛里的在线成员实时显示,或基于网页的聊天.会议系统等.这种情况下,如果用户点击"退出"按钮或链接,我们将之行一系列后 ...

  6. session 安全问题(关闭页面时自动清除session)

    要是直接关闭浏览器,并不直接触发SESION_ONEND事件,因此为了安全的需要,就需要调用页面关闭触发的事件onUnload ,利用这个事件来执行一个函数.在函数中调用session.abandon ...

  7. 关闭页面那点事儿...

    近日领导对于统计相关的工作高度关注,责成相关工作人员(我)真抓实干(加班加点),急群众(领导)之所急,想群众(领导)之所想,尽快落实相关工作...具体来说就是给我们的C端也加上统计代码,统计用户打开时 ...

  8. Android开发画布销毁,Android DialogFragment 在页面销毁下的使用方式

    今天看到了一篇文章,讲了DialogFragment的封装方式(Android:我为何要封装DialogFragment?),想到当初也为页面销毁后DialogFragment的回调方式头疼了好久,看 ...

  9. php关闭当前页_php如何直接关闭页面注销SESSION

    php如何直接关闭页面注销SESSION 发布时间:2020-07-09 09:03:06 来源:亿速云 阅读:100 作者:Leah 这篇文章将为大家详细讲解有关php如何直接关闭页面注销SESSI ...

最新文章

  1. Github阅览神器来啦!瞬间提升50%的阅览效率
  2. 1、在Centos上安装Grafana
  3. 如何编写高质量和可维护的代码
  4. python编程,外星人飞船
  5. 网站服务器windows登陆密码忘记,网站服务器windows登陆密码忘记
  6. PsTools在***中的一点小应用
  7. 如何配置一个最基本的web富文本编辑器?--之wangEditor(验证成功)
  8. 数据分析师免费课程网址
  9. IDEA编辑器多行编辑模式
  10. 整合Spring与Hibernate
  11. mpvue 初始化微信小程序
  12. 结构体赋值 -- 构造函数
  13. 基于考研的C语言与数据结构指北
  14. java+webrtc+回声消除,WebRTC回声消除(2)
  15. PDF怎么快速统计文档字数?这两种方法很简单
  16. 苹果查看电池实际容量
  17. 针对iPhone X和iPhone XS这些傻叉手机安全距离的设定
  18. SpringBoot application.properties读取属性配置文件中文显示为乱码问题的解决
  19. VS2019 KEY
  20. android studio distributionurl是干嘛的,不懂就学系列(一):gradle配置本地distributionUrl...

热门文章

  1. 销量破亿,董洁直播间凭何出圈?
  2. 给下拉框加上可输入查询特性-升级版本
  3. 常见SOC启动流程分析
  4. 干货 | Trip.com APP QUIC应用和优化实践
  5. 用js做一个鼠标惯性动画
  6. 今天听得好多老的电影的配乐啊
  7. 管家婆批量收款:提高收款效率的新功能
  8. 阿里M8每天肝到凌晨,竟是只为一份文档把分布式到微服务讲清楚
  9. SQL server已更新或删除的行值要么不能使该行成为唯一行,要么改变了多个行(2行)
  10. 如何在spss结果输出页面显示命令/运行代码