首页

android 侧滑删除 实例源码

java

2020-7-30

package com.exampledelete;

import android.content.Context;
import android.graphics.Rect;
import android.support.v4.app.Fragment;
import android.support.v4.widget.ViewDragHelper;
import android.support.v4.widget.ViewDragHelper.Callback;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewDebug;
import android.widget.FrameLayout;

public class SwipeLayout extends FrameLayout {

	private ViewDragHelper mDragHelper;
	private View frontView;
	private View backView;
	private int width;
	private int height;
	private int mRange;
	
	public SwipeLayout(Context context, AttributeSet attrs) {
		this(context, attrs,0);
	}

	public SwipeLayout(Context context) {
		this(context,null);
	}
	public SwipeLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mDragHelper=ViewDragHelper.create(this,1.5f,MCallBack());
	}

	  Callback MCallBack() {
		return new Callback() {
			
			@Override
			public boolean tryCaptureView(View arg0, int arg1) {
				return true;
			}
			//设置移动范围
			@Override
			public int clampViewPositionHorizontal(View child, int left, int dx) {
				if(child==frontView){
					if(left>0){
						left=0;
					}else if(left<-mRange){
						left=-mRange;
					}
				}else if(child==backView){
					if(left>width){
						left=width;
					}else if(left<width-mRange){
						left=width-mRange;
					}
				}
				return left;
			}
			@Override
			public void onViewPositionChanged(View changedView, int left,
					int top, int dx, int dy) {
				super.onViewPositionChanged(changedView, left, top, dx, dy);
				if(changedView==frontView){
					backView.offsetLeftAndRight(dx);
				}else if(changedView==backView){
					frontView.offsetLeftAndRight(dx);
				}
				// 兼容老版本
				invalidate();
			}
			
			
		};
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		return mDragHelper.shouldInterceptTouchEvent(ev);
	}
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	mDragHelper.processTouchEvent(event);
    	return true;
    }
    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
    		int bottom) {
    	super.onLayout(changed, left, top, right, bottom);
        LayoutView(false);
    }

	private void LayoutView(boolean isopen) {
		Rect frontRect = getFrontRect(isopen);
		frontView.layout(frontRect.left, frontRect.top, frontRect.right, frontRect.bottom);
		Rect BackRect = getbackRect(frontRect);
		backView.layout(BackRect.left, BackRect.top, BackRect.right, BackRect.bottom);
		// 调整顺序, 把mFrontView前置
		bringChildToFront(frontView);
	}
    
    private Rect getbackRect(Rect frontRect) {
    	int left = frontRect.right;
		return new Rect(left,0, left mRange, height);
	}

	private Rect getFrontRect(boolean isopen) {
    	int left = 0;
		if(isopen){
			left=-mRange;
		}
		return new Rect(left,0, left width, height);
	}

	@Override
    protected void onFinishInflate() {
    	super.onFinishInflate();
    	frontView = getChildAt(1);
    	backView = getChildAt(0);
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    	super.onSizeChanged(w, h, oldw, oldh);
    	width = frontView.getMeasuredWidth();
    	height = frontView.getMeasuredHeight();
    	mRange = backView.getMeasuredWidth();
    } 
}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
側拉删除/.classpath , 364
側拉删除/.project , 848
側拉删除/AndroidManifest.xml , 875
側拉删除/bin/AndroidManifest.xml , 875
側拉删除/bin/classes/com/exampledelete/BuildConfig.class , 343
側拉删除/bin/classes/com/exampledelete/MainActivity.class , 815
側拉删除/bin/classes/com/exampledelete/R$attr.class , 337
側拉删除/bin/classes/com/exampledelete/R$dimen.class , 458
側拉删除/bin/classes/com/exampledelete/R$drawable.class , 434
側拉删除/bin/classes/com/exampledelete/R$id.class , 540
側拉删除/bin/classes/com/exampledelete/R$layout.class , 400
側拉删除/bin/classes/com/exampledelete/R$menu.class , 385
側拉删除/bin/classes/com/exampledelete/R$string.class , 469
側拉删除/bin/classes/com/exampledelete/R$style.class , 428
側拉删除/bin/classes/com/exampledelete/R.class , 663
側拉删除/bin/classes/com/exampledelete/SwipeLayout$1.class , 1733
側拉删除/bin/classes/com/exampledelete/SwipeLayout.class , 3718
側拉删除/bin/classes.dex , 715848
側拉删除/bin/dexedLibs/android-support-v4-39ff4744bbf9b20f0f4f8c709264baa8.jar , 242080
側拉删除/bin/jarlist.cache , 119
側拉删除/bin/res/drawable-hdpi/head_1.png , 1183
側拉删除/bin/res/drawable-hdpi/ic_launcher.png , 5964
側拉删除/bin/res/drawable-mdpi/ic_launcher.png , 3112
側拉删除/bin/res/drawable-xhdpi/ic_launcher.png , 9355
側拉删除/bin/res/drawable-xxhdpi/ic_launcher.png , 17889
側拉删除/bin/resources.ap_ , 42693
側拉删除/bin/側拉删除.apk , 289207
側拉删除/gen/com/exampledelete/BuildConfig.java , 159
側拉删除/gen/com/exampledelete/R.java , 2804
側拉删除/ic_launcher-web.png , 51394
側拉删除/libs/android-support-v4.jar , 648327
側拉删除/proguard-project.txt , 781
側拉删除/project.properties , 563
側拉删除/res/drawable-hdpi/head_1.png , 1183
側拉删除/res/drawable-hdpi/ic_launcher.png , 7658
側拉删除/res/drawable-mdpi/ic_launcher.png , 3777
側拉删除/res/drawable-xhdpi/ic_launcher.png , 12516
側拉删除/res/drawable-xxhdpi/ic_launcher.png , 24777
側拉删除/res/layout/activity_main.xml , 2190
側拉删除/res/menu/main.xml , 261
側拉删除/res/values/dimens.xml , 218
側拉删除/res/values/strings.xml , 227
側拉删除/res/values/styles.xml , 695
側拉删除/res/values-sw600dp/dimens.xml , 201
側拉删除/res/values-sw720dp-land/dimens.xml , 275
側拉删除/res/values-v11/styles.xml , 332
側拉删除/res/values-v14/styles.xml , 389
側拉删除/src/com/exampledelete/MainActivity.java , 531
側拉删除/src/com/exampledelete/SwipeLayout.java , 3480
没有账号? 忘记密码?

社交账号快速登录