【转载】android仿QQ列表的效果实现

转载请注明出处,谢谢:http://blog.csdn.net/tomatozq/article/details/7560232


本人在该作者源码基础上对其onTouchEvent进行了部分修改:

private float mDownX;
 	private float mDownY;
 	private boolean isFingerFirstTouchHeaderView = false;
 	private boolean isFingerLongMove = false;
 
 	/**
 	 * 如果 HeaderView 是可见的 , 此函数用于判断是否点击了 HeaderView, 并对做相应的处理 ,
 	 * 因为 HeaderView 是画上去的 , 所以设置事件监听是无效的 , 只有自行控制 .
 	 */
 	@Override
 	public boolean onTouchEvent(MotionEvent ev) {
 		if (mHeaderViewVisible) {
 			mDownX = ev.getX();
 			mDownY = ev.getY();
 //			Log.e("", "ev:"+ev.getAction()+", mDownX:"+(int)mDownX+", mDownY:"+(int)mDownY+", isFingerFirstTouchHeaderView:"+isFingerFirstTouchHeaderView+", isFingerLongMove:"+isFingerLongMove);
 			switch (ev.getAction()) {
 			case MotionEvent.ACTION_DOWN:
 				if (mDownX <= mHeaderViewWidth && mDownY <= mHeaderViewHeight) {
 					isFingerFirstTouchHeaderView = true;
 //					Log.i("", "mHeaderView group_bg_pressed");
 					mHeaderView.setBackgroundResource(R.drawable.group_bg_pressed);
 					invalidate();
 					return true;
 				}
 				break;
 			case MotionEvent.ACTION_MOVE:
 				if((mDownX > mHeaderViewWidth || mDownY > mHeaderViewHeight)
 						&& isFingerFirstTouchHeaderView){
 					isFingerLongMove = true;
 //					Log.i("", "mHeaderView group_bg_normal move");
 					mHeaderView.setBackgroundResource(R.drawable.group_bg_normal);
 					invalidate();
 					return true;
 				}
 				break;
 			case MotionEvent.ACTION_UP:
 				if(isFingerFirstTouchHeaderView && !isFingerLongMove){
 //					Log.i("", "mHeaderView group_bg_normal up");
 					mHeaderView.setBackgroundResource(R.drawable.group_bg_normal);
 					invalidate();
 					float x = ev.getX();
 					float y = ev.getY();
 					float offsetX = Math.abs(x - mDownX);
 					float offsetY = Math.abs(y - mDownY);
 					// 如果 HeaderView 是可见的 , 点击在 HeaderView 内 , 那么触发 headerClick()
 					if (x <= mHeaderViewWidth && y <= mHeaderViewHeight
 							&& offsetX <= mHeaderViewWidth && offsetY <= mHeaderViewHeight) {
 						if (mHeaderView != null) {
 							headerViewClick();
 						}
 
 						resetParams();
 						return true;
 					}
 				}
 				
 				if(isFingerFirstTouchHeaderView
 						&& isFingerLongMove){
 					mHeaderView.setBackgroundResource(R.drawable.group_bg_normal);
 					invalidate();
 					resetParams();
 					return true;
 				}
 				
 				resetParams();
 				break;
 			default:
 				break;
 			}
 		}
 
 		return super.onTouchEvent(ev);
 	}
 	
 	private void resetParams(){
 		isFingerLongMove = false;
 		isFingerFirstTouchHeaderView = false;
 	}


增加了对mHeaderView的触摸监听,模拟了触摸之后手指移动至别处产生无效按键事件。


你可能感兴趣的:(Android,JAVA,android,qq,float,up,null)