int firstVisiblePos = getFirstVisiblePosition() - getHeaderViewsCount(); int factPos = curPos - firstVisiblePos; mItemView = getChildAt(factPos);
@Override
public boolean onTouchEvent(MotionEvent event) {
if(!mEnableSliding)
return false;
if(mCancelMotionEvent && event.getAction() == MotionEvent.ACTION_MOVE) {
return true;
} else if(mCancelMotionEvent && event.getAction() == MotionEvent.ACTION_DOWN) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN: {
if(mTracker == null)
mTracker = VelocityTracker.obtain();
else
mTracker.clear();
mLastMotionX = event.getX();
mLastMotionY = event.getY();
}break;
case MotionEvent.ACTION_MOVE: {
mTracker.addMovement(event);
mTracker.computeCurrentVelocity(1000);
int curVelocityX = (int) mTracker.getXVelocity();
float curX = event.getX();
float curY = event.getY();
int lastPos = pointToPosition(
(int)mLastMotionX, (int)mLastMotionY);
int curPos = pointToPosition((int)curX, (int)curY);
int distanceX = (int)(mLastMotionX - curX);
if(lastPos == curPos && (distanceX >= MAX_DISTANCE || curVelocityX < -MAX_FLING_VELOCITY)) {
int firstVisiblePos = getFirstVisiblePosition() - getHeaderViewsCount();
int factPos = curPos - firstVisiblePos;
mItemView = getChildAt(factPos);
if(mItemView != null) {
if(mButtonID == -1)
throw new IllegalButtonIDException("Illegal DeleteButton resource id,"
+ "ensure excute the function setButtonID(int id)");
mButton = mItemView.findViewById(mButtonID);
mButton.setVisibility(View.VISIBLE);
mButton.startAnimation(mShowAnim);
mLastButtonShowingPos = curPos;
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mDeleteItemListener != null)
mDeleteItemListener.onButtonClick(v, mLastButtonShowingPos);
mButton.setVisibility(View.GONE);
mLastButtonShowingPos = -1;
}
});
mCancelMotionEvent = true;
}
}
}break;
case MotionEvent.ACTION_UP: {
if(mTracker != null) {
mTracker.clear();
mTracker.recycle();
mTracker = null;
}
mCancelMotionEvent = false;
if(mLastButtonShowingPos != -1) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
}break;
case MotionEvent.ACTION_CANCEL: {
hideShowingButtonWithAnim();
}break;
}
return super.onTouchEvent(event);
}
mButton.getLocationOnScreen(mShowingButtonLocation);
int left = mShowingButtonLocation[0];
int right = mShowingButtonLocation[0] + mButton.getWidth();
int top = mShowingButtonLocation[1];
int bottom = mShowingButtonLocation[1] + mButton.getHeight();
return (ev.getRawX() >= left
&& ev.getRawX() <= right
&& ev.getRawY() >= top
&& ev.getRawY() <= bottom);
} 接下来重写onInterceptTouchEvent()方法:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(mEnableSliding && mLastButtonShowingPos != -1 &&
ev.getAction() == MotionEvent.ACTION_DOWN && !isClickButton(ev)) {
ev.setAction(MotionEvent.ACTION_CANCEL);
mCancelMotionEvent = true;
return true;
}
return super.onInterceptTouchEvent(ev);
};
else if(mCancelMotionEvent && event.getAction() == MotionEvent.ACTION_DOWN) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
if(mLastButtonShowingPos != -1) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有