前言:
用最簡單的例子來說明此問題。
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接受一下。然後直接關閉就行。
其實也沒有什麼,幫助想要該功能的童鞋。
有什麼問題可以跟帖詢問。
補上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>