写这篇文章是因为看了:http://blog.csdn.net/jdsjlzx/article/details/21231217这篇文章,并下载了demo运行了下。
现对其做了一些修改:
(1) 不在通过setTouchView来处理事件,采用事件拦截机制
(2)加入了右侧滑动机制
(3)不在对整个activity的触摸都有效,采用了触摸点判断,使其只是左右边界时才有效,从而是activity的触摸事件和listview等控件的滚动相分开
(4)加入左右滑动切换机制的禁用与开启功能,使其更方便
本文只贴上部分代码,不做原理讲述,如果你没有看上一篇:Android 向右滑动销毁(finish)Activity, 随着手势的滑动而滑动的效果(一),请先看一遍。
demo中使用需要注意的地方,在代码中都有注释,有任何问题欢迎联系。
1、activity布局,根节点使用定义的view(只对需要滑动的布局加即可)
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#88ff90" >
2、SildingFinishLayout的实现
public class SildingFinishLayout extends RelativeLayout{
private final String TAG = SildingFinishLayout.class.getName();
/**
* SildingFinishLayout布局的父布局
*/
private ViewGroup mParentView;
/**
* 滑动的最小距离
*/
private int mTouchSlop;
/**
* 按下点的X坐标
*/
private int downX;
/**
* 按下点的Y坐标
*/
private int downY;
/**
* 临时存储X坐标
*/
private int tempX;
/**
* 滑动类
*/
private Scroller mScroller;
/**
* SildingFinishLayout的宽度
*/
private int viewWidth;
/**
* 记录是否正在滑动
*/
private boolean isSilding;
private OnSildingFinishListener onSildingFinishListener;
private boolean enableLeftSildeEvent = true; //是否开启左侧切换事件
private boolean enableRightSildeEvent = true; // 是否开启右侧切换事件
private final int size = 20; //按下时范围(处于这个范围内就启用切换事件,目的是使当用户从左右边界点击时才响应)
private boolean isIntercept = false; //是否拦截触摸事件
private boolean canSwitch;//是否可切换
private boolean isSwitchFromLeft = false; //左侧切换
private boolean isSwitchFromRight = false; //右侧侧切换
public SildingFinishLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SildingFinishLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Log.e(TAG, "设备的最小滑动距离:" + mTouchSlop);
mScroller = new Scroller(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (changed) {
// 获取SildingFinishLayout所在布局的父布局
mParentView = (ViewGroup) this.getParent();
viewWidth = this.getWidth();
}
Log.e(TAG, "viewWidth=" + viewWidth);
}
public void setEnableLeftSildeEvent(boolean enableLeftSildeEvent) {
this.enableLeftSildeEvent = enableLeftSildeEvent;
}
public void setEnableRightSildeEvent(boolean enableRightSildeEvent) {
this.enableRightSildeEvent = enableRightSildeEvent;
}
/**
* 设置OnSildingFinishListener, 在onSildingFinish()方法中finish Activity
*
* @param onSildingFinishListener
*/
public void setOnSildingFinishListener(
OnSildingFinishListener onSildingFinishListener) {
this.onSildingFinishListener = onSildingFinishListener;
}
//是否拦截事件,如果不拦截事件,对于有滚动的控件的界面将出现问题(相冲突)
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
float downX = ev.getRawX();
Log.e(TAG, "downX =" + downX + ",viewWidth=" + viewWidth);
if(enableLeftSildeEvent && downX < size){
Log.e(TAG, "downX 在左侧范围内 ,拦截事件");
isIntercept = true;
isSwitchFromLeft = true;
isSwitchFromRight = false;
return true;
}else if(enableRightSildeEvent && downX > (viewWidth - size)){
Log.e(TAG, "downX 在右侧范围内 ,拦截事件");
isIntercept = true;
isSwitchFromRight = true;
isSwitchFromLeft = false;
return true;
}else{
Log.e(TAG, "downX 不在范围内 ,不拦截事件");
isIntercept = false;
isSwitchFromLeft = false;
isSwitchFromRight = false;
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(!isIntercept){//不拦截事件时 不处理
return false;
}
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
downX = tempX = (int) event.getRawX();
downY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveX = (int) event.getRawX();
int deltaX = tempX - moveX;
tempX = moveX;
if (Math.abs(moveX - downX) > mTouchSlop && Math.abs((int) event.getRawY() - downY) < mTouchSlop) {
isSilding = true;
}
Log.e(TAG, "scroll deltaX=" + deltaX);
if(enableLeftSildeEvent){//左侧滑动
if (moveX - downX >= 0 && isSilding) {
mParentView.scrollBy(deltaX, 0);
}
}
if(enableRightSildeEvent){//右侧滑动
if (moveX - downX <= 0 && isSilding) {
mParentView.scrollBy(deltaX, 0);
}
}
Log.e(TAG + "/onTouchEvent", "mParentView.getScrollX()=" + mParentView.getScrollX());
break;
case MotionEvent.ACTION_UP:
isSilding = false;
//mParentView.getScrollX() <= -viewWidth / 2 ==>指左侧滑动
//mParentView.getScrollX() >= viewWidth / 2 ==>指右侧滑动
if (mParentView.getScrollX() <= -viewWidth / 2 || mParentView.getScrollX() >= viewWidth / 2) {
canSwitch = true;
if(isSwitchFromLeft){
scrollToRight();
}
if(isSwitchFromRight){
scrollToLeft();
}
} else {
scrollOrigin();
canSwitch = false;
}
break;
}
return true;
}
/**
* 滚动出界面至右侧
*/
private void scrollToRight() {
final int delta = (viewWidth + mParentView.getScrollX());
// 调用startScroll方法来设置一些滚动的参数,我们在computeScroll()方法中调用scrollTo来滚动item
mScroller.startScroll(mParentView.getScrollX(), 0, -delta + 1, 0, Math.abs(delta));
postInvalidate();
}
/**
* 滚动出界面至左侧
*/
private void scrollToLeft() {
final int delta = (viewWidth - mParentView.getScrollX());
// 调用startScroll方法来设置一些滚动的参数,我们在computeScroll()方法中调用scrollTo来滚动item
mScroller.startScroll(mParentView.getScrollX(), 0, delta - 1, 0, Math.abs(delta));//此处就不可用+1,也不卡直接用delta
postInvalidate();
}
/**
* 滚动到起始位置
*/
private void scrollOrigin() {
int delta = mParentView.getScrollX();
mScroller.startScroll(mParentView.getScrollX(), 0, -delta, 0,
Math.abs(delta));
postInvalidate();
}
@Override
public void computeScroll(){
// 调用startScroll的时候scroller.computeScrollOffset()返回true,
if (mScroller.computeScrollOffset()) {
mParentView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
if (mScroller.isFinished()) {
if (onSildingFinishListener != null && canSwitch) {
Log.e(TAG, "mScroller finish");
if(isSwitchFromLeft){//回调,左侧切换事件
onSildingFinishListener.onSildingBack();
}
if(isSwitchFromRight){//右侧切换事件
onSildingFinishListener.onSildingForward();
}
}
}
}
}
public interface OnSildingFinishListener {
public void onSildingBack();
public void onSildingForward();
}
}
3、BaseActivity
/**
* 1. 必须设置透明风格,设置透明后需对Activity设置一个背景色,其遮盖效果
* 2. 要调用initBaseActivity(boolean enableLeftSilde, boolean enableRigftSilde)
*
* @ enableLeftSilde : 开启左侧
* @ enableRightSilde : 开启右侧
*
* */
public class BaseActivity extends Activity implements OnSildingFinishListener{
private SildingFinishLayout sildingLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* @enableLeftSilde : 开启左侧
* @enableRightSilde : 开启右侧
* */
public void initBaseActivity(boolean enableLeftSilde, boolean enableRigftSilde){
sildingLayout = (SildingFinishLayout) findViewById(R.id.slidingLayout);
sildingLayout.setOnSildingFinishListener(this);
sildingLayout.setEnableLeftSildeEvent(enableLeftSilde);
sildingLayout.setEnableRightSildeEvent(enableRigftSilde);
}
@Override
public void onSildingBack() {
Toast.makeText(getApplicationContext(), "back", Toast.LENGTH_SHORT).show();
finish();
}
@Override
public void onSildingForward() {
Toast.makeText(getApplicationContext(), "前进", Toast.LENGTH_SHORT).show();
finish();
}
}
欢迎有需要的朋友下载,0积分,有问题相互交流。
下载地址: Activity滑动跳转