Android 侧滑删除功能

重定义listview

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

public class SliderListView extends ListView {
	private static final String TAG = "SliderListView";
	private float mX = 0;
	private float mY = 0;
	private int mPosition = -1;
	private boolean isSlider = false;
	private SlideView mFocusedItemView;

	public SliderListView(Context context) {
		super(context);
	}

	public SliderListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SliderListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			isSlider = false;
			mX = x;
			mY = y;
			int position = pointToPosition((int) x, (int) y);
			if (mPosition != position) {
				mPosition = position;
				if (mFocusedItemView != null) {
					mFocusedItemView.reset();
				}
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if (mPosition != -1) {
				if (Math.abs(mY - y) < 30 && Math.abs(mX - x) > 20) {
					int first = this.getFirstVisiblePosition();
					int index = mPosition - first;
					mFocusedItemView = (SlideView) getChildAt(index);
					// mFocusedItemView = (SlideView)
					// getItemAtPosition(mPosition);
					mFocusedItemView.onTouchEvent(event);
					isSlider = true;
					return true;
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			if (isSlider) {
				isSlider = false;
				if (mFocusedItemView != null) {
					mFocusedItemView.adjust(mX - x > 0);
					return true;
				}
			}
			break;
		}
		return super.onTouchEvent(event);
	}
}


滑动view

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;

import com.future.wodownloadmanager.R;

public class SlideView extends LinearLayout {
	private static final String TAG = "SlideView";
	private static final int TAN = 2;
	private int mHolderWidth = 120;
	private float mLastX = 0;
	private float mLastY = 0;
	private LinearLayout mViewContent;
	private Scroller mScroller;
	private Context mContext;
	private Resources mResources;

	private SlideView(Context context) {
		super(context);
		initView(null, null, null);
	}

	public SlideView(Context context, Resources resources, View content) {
		super(context);
		this.mContext = context;
		this.mResources = resources;
		initView(context, resources, content);
	}

	private SlideView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(null, null, null);
	}

	private void initView(Context context, Resources resources, View content) {
		setOrientation(LinearLayout.HORIZONTAL);
		mScroller = new Scroller(context);
		LayoutInflater.from(context).inflate(
				resources.getLayout(R.layout.slide_view_merge), this);
		findViewById(R.id.holder).setBackground(
				resources.getDrawable(R.drawable.selector_slider_holder));
		TextView delete = (TextView) findViewById(R.id.delete);
		delete.setCompoundDrawablesWithIntrinsicBounds(
				resources.getDrawable(R.drawable.ic_delete), null, null, null);
		mViewContent = (LinearLayout) findViewById(R.id.view_content);
		mHolderWidth = Math.round(TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, mHolderWidth, getResources()
						.getDisplayMetrics()));
		if (content != null) {
			mViewContent.addView(content);
		}
	}

	public void shrink() {
		int offset = getScrollX();
		if (offset == 0) {
			return;
		}
		scrollTo(0, 0);
	}

	public void setContentView(View view) {
		if (mViewContent != null) {
			mViewContent.addView(view);
		}
	}

	public void reset() {
		int offset = getScrollX();
		if (offset == 0) {
			return;
		}
		smoothScrollTo(0, 0);
	}

	public void adjust(boolean left) {
		int offset = getScrollX();
		if (offset == 0) {
			return;
		}
		if (offset < 20) {
			this.smoothScrollTo(0, 0);
		} else if (offset < mHolderWidth - 20) {
			if (left) {
				this.smoothScrollTo(mHolderWidth, 0);
			} else {
				this.smoothScrollTo(0, 0);
			}
		} else {
			this.smoothScrollTo(mHolderWidth, 0);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_MOVE:
			float x = event.getX();
			float y = event.getY();
			float deltaX = x - mLastX;
			float delatY = y - mLastY;
			mLastX = x;
			mLastY = y;
			if (Math.abs(deltaX) < Math.abs(delatY) * TAN) {
				break;
			}
			if (deltaX != 0) {
				float newScrollX = getScrollX() - deltaX;
				if (newScrollX < 0) {
					newScrollX = 0;
				} else if (newScrollX > mHolderWidth) {
					newScrollX = mHolderWidth;
				}
				this.scrollTo((int) newScrollX, 0);
			}
			break;
		}
		return super.onTouchEvent(event);
	}

	private void smoothScrollTo(int destX, int destY) {
		int scrollX = getScrollX();
		int delta = destX - scrollX;
		mScroller.startScroll(scrollX, 0, delta, 0, Math.abs(delta) * 3);
		invalidate();
	}

	@Override
	public void computeScroll() {
		if (mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			postInvalidate();
		}
	}
}


获取view是需要重置缓存状态

public View getView(final int position, View convertView,
ViewGroup parent) 


slideview.shrink()


布局文件




    
    

    

        
    





    
    
    
    


    #E84A40
    #E22F2F


Android 侧滑删除功能_第1张图片



代码:http://download.csdn.net/detail/hknock/8877003



你可能感兴趣的:(android)