Android 向右滑动关闭页面
前言:
用最简单的例子来说明此问题。
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); } @Override public 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); } @Override public 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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); isFinishScrollLeft = true; //延后为了等待需要屏蔽返回滑动接受参数 new Handler().postAtTime(new Runnable() { @Override public 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); } } } @Override public 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接受一下。然后直接关闭就行。
其实也没有什么,帮助想要该功能的童鞋。
有什么问题可以跟帖询问。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。