Code example for GridView

Methods: getChildAt, getFirstVisiblePosition, pointToPosition

0
	@Override 
	public boolean onTouch(View v, MotionEvent event) {
 
		int x = (int) event.getX();
		int y = (int) event.getY();
		int position = mGridView.pointToPosition(x, y);
		 
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			//����xy����û�е�����ͼƬ��ʱ��position=-1�� 
			if (position == -1) {
				break; 
			} 
			from = position;
			 
			//��ȡ��������View 
			View tempView = mGridView.getChildAt(position
					- mGridView.getFirstVisiblePosition());
			 
			 
			 
			if (tempView == null) {
				break; 
			} 
			tempView.setDrawingCacheEnabled(true);
			 
			mDragViewX = x - tempView.getLeft();
			mDragViewY = y - tempView.getTop();
			 
			//tempView.getTop()��ȡ��tempView������λ�ã���Ϊ���µ�ʱ��Ӧ����ʾ��λ�ú���������λ����ͬ 
			//tempView.getLeft() ͬ�� 
			 
			startDragging(tempView.getDrawingCache(), tempView.getTop(),
					tempView.getLeft());
			 
			tempView.setVisibility(View.INVISIBLE);
			break; 
		case MotionEvent.ACTION_MOVE:
			Log.i("ivan", "1 end");
			if (from == -1) {
				break; 
			} 
			if (mDragView != null) {
				to = position;
				Log.i("ivan", "to = ======" + to);
				RelativeLayout.LayoutParams rllp = (LayoutParams) mDragView.getLayoutParams();
				rllp.leftMargin = (int) (x - mDragViewX);
				rllp.topMargin = (int) (y - mDragViewY);
				 
				 
				mDragView.setLayoutParams(rllp);
				//�򶯻���ʱ�������ٴθı� 
				if (from != to && to != -1 ) {
 
					aniFrom = from;
					aniTo = to;
					 
					Map<String, Object> temp = mAdapter.getItem(from);
					mAdapter.remove(temp);
 					mAdapter.insert(temp, to);
					doingAni = true;
					from = to;
				} 
			} 
			break; 
		case MotionEvent.ACTION_UP:
			View end = mGridView.getChildAt(from
					- mGridView.getFirstVisiblePosition());
			if (end != null) {
				end.setVisibility(View.VISIBLE);
			} 
			rl.removeView(mDragView);
			break;