ListView特效2(列表拖拽)
16lz
2021-01-26
即listView特效1,http://blog.csdn.net/pipisky2006/article/details/7393475,好久之后
列表的拖拽效果(参考Android源码下packages/apps/Music中的播放列表TouchInterceptor.java源码)
重写ListView中onInterceptTouchEvent(),onTouchEvent()方法来响应触控事件做相应的界面调整(选中,生成影像,拖动影像,数据更改后刷新界面)等等。
拖拽的动作实际上是WindowManager在最上层添加的ImageView随着手指移动被拖动。listView作为背景,数据随之变化。
下面是源码的分析
public class TouchInterceptor extends ListView {//继承自ListView public TouchInterceptor(Context context, AttributeSet attrs) { super(context, attrs); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();//检测滑动状态的阀值 Resources res = getResources(); mItemHeightNormal = res.getDimensionPixelSize(R.dimen.normal_height); mItemHeightHalf = mItemHeightNormal / 2; mItemHeightExpanded = res.getDimensionPixelSize(R.dimen.expanded_height); }}
下面是用到的变量:
private ImageView mDragView;//被拖拽的ImageView private WindowManager mWindowManager; private WindowManager.LayoutParams mWindowParams; private int mDragPos; // which item is being dragged 被拖拽的View所在的当前的position private int mFirstDragPos; // where was the dragged item originally 被拖拽的View的最初的position private int mDragPoint; // at what offset inside the item did the user grab it 在当前数据项中的位置 private int mCoordOffset; // the difference between screen coordinates and coordinates in this view 屏幕坐标和View坐标的差值,也就是getRawY和getY的差值 private int mUpperBound; //拖动的时候,开始向上滚动的边界 private int mLowerBound; //拖动的时候,开始向下滚动的边界 private int mHeight; private Rect mTempRect = new Rect(); private Bitmap mDragBitmap; private final int mTouchSlop; //判断滑动的一个距离值 private int mItemHeightNormal; private int mItemHeightExpanded; private int mItemHeightHalf;
捕捉down事件,在down事件中,判断是否需要拖动,如果是,我们做一些拖动的准备工作,准备影像。
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN://down事件 int x = (int) ev.getX(); int y = (int) ev.getY(); int itemnum = pointToPosition(x, y);//选中的数据项位置,使用ListView自带的pointToPosition(x, y)方法 if (itemnum == AdapterView.INVALID_POSITION) { break; } ViewGroup item = (ViewGroup) getChildAt(itemnum - getFirstVisiblePosition());//获取选中的View项 mDragPoint = y - item.getTop();//点击位置在点击View内的相对位置 mCoordOffset = ((int)ev.getRawY()) - y; View dragger = item.findViewById(R.id.icon);//选中项中的拖动识别View Rect r = mTempRect; dragger.getDrawingRect(r); // The dragger icon itself is quite small, so pretend the touch area is bigger if (x < r.right * 2) { item.setDrawingCacheEnabled(true); // Create a copy of the drawing cache so that it does not get recycled // by the framework when the list tries to clean up memory Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());//获取拖动影像 startDragging(bitmap, y);//准备拖动工作 mDragPos = itemnum; mFirstDragPos = mDragPos; mHeight = getHeight(); int touchSlop = mTouchSlop; mUpperBound = Math.min(y - touchSlop, mHeight / 3);//当在屏幕的上部(上面1/3区域)或者更上的区域,执行拖动的边界,下同理定义 mLowerBound = Math.max(y + touchSlop, mHeight * 2 /3); return false; } stopDragging(); break; } return super.onInterceptTouchEvent(ev); }
//根据操作处理滑动和放置操作。
@Override public boolean onTouchEvent(MotionEvent ev) { if (mDragView != null) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: Rect r = mTempRect; mDragView.getDrawingRect(r); stopDragging();//释放拖动影像 if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount()) { mDropListener.drop(mFirstDragPos, mDragPos);//放置工作的回调 } unExpandViews(false);//放置后List内容的修正 break; case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: int x = (int) ev.getX(); int y = (int) ev.getY(); mWindowParams.y = y - mDragPoint + mCoordOffset;//更新坐标位置 mWindowManager.updateViewLayout(mDragView, mWindowParams);//更新拖拽view的界面 int itemnum = getItemForPosition(y);//获取准确的position,考虑不可见view等情况,具体见其私有方法 if (itemnum >= 0) { if (action == MotionEvent.ACTION_DOWN || itemnum != mDragPos) { if (mDragListener != null) { mDragListener.drag(mDragPos, itemnum);//拖动工作的回调 } mDragPos = itemnum; doExpansion();// } int speed = 0; adjustScrollBounds(y);//调整滚动的阀值 if (y > mLowerBound) {//拖动位置偏下,内容向上滚动 // scroll the list up a bit speed = y > (mHeight + mLowerBound) / 2 ? 16 : 4; } else if (y < mUpperBound) {//拖动位置偏上,内容向下滚动 // scroll the list down a bit speed = y < mUpperBound / 2 ? -16 : -4; } if (speed != 0) { int ref = pointToPosition(0, mHeight / 2); if (ref == AdapterView.INVALID_POSITION) { //we hit a divider or an invisible view, check somewhere else ref = pointToPosition(0, mHeight / 2 + getDividerHeight() + 64); } View v = getChildAt(ref - getFirstVisiblePosition()); if (v!= null) { int pos = v.getTop(); setSelectionFromTop(ref, pos - speed);//列表滚动 } } } break; } return true; } return super.onTouchEvent(ev); }
下面是具体的准备拖动影像和释放拖动影像的方法
private void startDragging(Bitmap bm, int y) { stopDragging(); mWindowParams = new WindowManager.LayoutParams(); mWindowParams.gravity = Gravity.TOP; mWindowParams.x = 0; mWindowParams.y = y - mDragPoint + mCoordOffset;//计算拖动的起始位置 mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT; mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT; mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; mWindowParams.format = PixelFormat.TRANSLUCENT; mWindowParams.windowAnimations = 0; Context context = getContext(); ImageView v = new ImageView(context); int backGroundColor = context.getResources().getColor(R.color.dragndrop_background); v.setBackgroundColor(backGroundColor); v.setImageBitmap(bm); mDragBitmap = bm; mWindowManager = (WindowManager)context.getSystemService("window"); mWindowManager.addView(v, mWindowParams);//添加到窗口上 mDragView = v; } //释放 private void stopDragging() { if (mDragView != null) { WindowManager wm = (WindowManager)getContext().getSystemService("window"); wm.removeView(mDragView); mDragView.setImageDrawable(null);//释放Imageview mDragView = null; } if (mDragBitmap != null) {//释放bitmap mDragBitmap.recycle(); mDragBitmap = null; } }
接下来响应操作是拖拽影像,影像动了后背后的列表内容也要配合变化,主要就是添加了个空内容格
/** Adjust visibility and size to make it appear as though * an item is being dragged around and other items are making * room for it: 调整可见性和大小来表现的像一个内容项被拖动,其他在给他让位置。 * If dropping the item would result in it still being in the * same place, then make the dragged listitem's size normal, * but make the item invisible. * Otherwise, if the dragged listitem is still on screen, make * it as small as possible and expand the item below the insert * point. * If the dragged item is not on screen, only expand the item * below the current insertpoint. */ private void doExpansion() { int childnum = mDragPos - getFirstVisiblePosition(); if (mDragPos > mFirstDragPos) { childnum++; } View first = getChildAt(mFirstDragPos - getFirstVisiblePosition()); for (int i = 0;; i++) { View vv = getChildAt(i); if (vv == null) { break; } int height = mItemHeightNormal; int visibility = View.VISIBLE; if (vv.equals(first)) { // processing the item that is being dragged if (mDragPos == mFirstDragPos) { // hovering over the original location visibility = View.INVISIBLE; } else { // not hovering over it height = 1; } } else if (i == childnum) { if (mDragPos < getCount() - 1) { height = mItemHeightExpanded; } } ViewGroup.LayoutParams params = vv.getLayoutParams(); params.height = height; vv.setLayoutParams(params); vv.setVisibility(visibility); } }
放置后的操作
/** * Restore size and visibility for all listitems */ private void unExpandViews(boolean deletion) { for (int i = 0;; i++) { View v = getChildAt(i); if (v == null) { if (deletion) { // HACK force update of mItemCount int position = getFirstVisiblePosition(); int y = getChildAt(0).getTop(); setAdapter(getAdapter()); setSelectionFromTop(position, y); // end hack } layoutChildren(); // force children to be recreated where needed v = getChildAt(i); if (v == null) { break; } } ViewGroup.LayoutParams params = v.getLayoutParams(); params.height = mItemHeightNormal; v.setLayoutParams(params); v.setVisibility(View.VISIBLE); } }
剩下的几个私有方法
/** * pointToPosition() doesn't consider invisible views, but we * need to, so implement a slightly different version. */ private int myPointToPosition(int x, int y) { if (y < 0) { // when dragging off the top of the screen, calculate position // by going back from a visible item int pos = myPointToPosition(x, y + mItemHeightNormal); if (pos > 0) { return pos - 1; } } Rect frame = mTempRect; final int count = getChildCount(); for (int i = count - 1; i >= 0; i--) { final View child = getChildAt(i); child.getHitRect(frame); if (frame.contains(x, y)) { return getFirstVisiblePosition() + i; } } return INVALID_POSITION; } private int getItemForPosition(int y) { int adjustedy = y - mDragPoint - mItemHeightHalf; int pos = myPointToPosition(0, adjustedy); if (pos >= 0) { if (pos <= mFirstDragPos) { pos += 1; } } else if (adjustedy < 0) { // this shouldn't happen anymore now that myPointToPosition deals // with this situation pos = 0; } return pos; } private void adjustScrollBounds(int y) { if (y >= mHeight / 3) { mUpperBound = mHeight / 3; } if (y <= mHeight * 2 / 3) { mLowerBound = mHeight * 2 / 3; } }
参考: http://www.cnblogs.com/qianxudetianxia/archive/2011/06/12/2068761.html
http://www.cnblogs.com/qianxudetianxia/archive/2011/06/13/2079253.html
http://www.oschina.net/code/explore/android-2.2-froyo/com/android/music/TouchInterceptor.java
更多相关文章
- android应用程序中获取view的位置(2)
- 【转】申请 android google map API key
- android view中touch的执行顺序
- 拖动 view
- TextUtils
- Android(安卓)拖动条(SeekBar)实例 附完整demo项目代码
- Android:如何将位置信息模式默认设置为高精确度
- android提供的TextUtils类
- Android(安卓)3D模型展示