本文主要是介绍自定义可拖拽的GridView,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转转请注明出处 http://blog.csdn.net/u011510784/article/details/51524900
自定义可拖拽的GridView,如图:
自定义可以拖拽的Gridview,继承 Gridview:
public class DraggableGridView extends GridView {public DraggableGridView(Context context) {super(context);this.mContext = context;}public DraggableGridView(Context context, AttributeSet attrs) {super(context, attrs);this.mContext = context;}}
@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {// 拦截按下动作if (ev.getAction() == MotionEvent.ACTION_DOWN) {return setOnItemLongClickListener(ev);}return super.onInterceptTouchEvent(ev);}
处理长按时间:
public boolean setOnItemLongClickListener(final MotionEvent ev) {this.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {。。。return false;};});return super.onInterceptTouchEvent(ev);}
这里主要操作是记录长按的位置,并将长按的那个ItemView悬浮显示,主要代码:
int offset=5;ViewGroup itemView = (ViewGroup) getChildAt(dragPosition- getFirstVisiblePosition());itemView.destroyDrawingCache();itemView.setDrawingCacheEnabled(true);itemView.setDrawingCacheBackgroundColor(0xff6DB7ED);Bitmap bm = Bitmap.createBitmap(itemView.getDrawingCache(true));Bitmap bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth() - offset, bm.getHeight() - offset);startDrag(bitmap, x, y);
private void startDrag(Bitmap bm, int x, int y) {stopDrag();windowParams = new WindowManager.LayoutParams();windowParams.gravity = Gravity.TOP | Gravity.LEFT;windowParams.x = dragItemView.getLeft();windowParams.y = dragItemView.getTop()+ (int) (45 * Configure.screenDensity);windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;windowParams.alpha = 0.8f;ImageView iv = new ImageView(getContext());iv.setImageBitmap(bm);windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);// 把影像ImagView添加到当前视图中windowManager.addView(iv, windowParams);dragImageView = iv;}
然后监听滑动事件,并相应的显示移动的动画效果,然后在手指抬起时结束整个过程,刷新布局:
@Overridepublic boolean onTouchEvent(MotionEvent ev) {if (dragImageView != null&& dragPosition != AdapterView.INVALID_POSITION) {// 控制父控件拦截事件getParent().requestDisallowInterceptTouchEvent(true);int x = (int) ev.getX();int y = (int) ev.getY();switch (ev.getAction()) {case MotionEvent.ACTION_MOVE:if (!isCountXY) {// 移动到哪个位置xtox = x - mLastX;ytoy = y - mLastY;isCountXY = true;}onDrag(x, y);if (!isMoving)onMove(x, y);break;case MotionEvent.ACTION_UP:stopDrag();onDrop(x, y);break;}}return super.onTouchEvent(ev);}
onDrag()方法用来根据手指的滑动来动态滑动我们在上面新建的那个ItemView:
private void onDrag(int x, int y) {// 计算位置,通过windowManager不停地更新Viewif (dragImageView != null) {windowParams.alpha = 0.8f;windowParams.x = (x - mLastX - xtox) + dragItemView.getLeft();windowParams.y = (y - mLastY - ytoy) + dragItemView.getTop() + 200+ (int) (45 * Configure.screenDensity);windowManager.updateViewLayout(dragImageView, windowParams);}}
onMove()方法用来根据手指滑动来动态滑动Gridview中的item,并且通过OnDragChangedListener接口通知Adapter进行相应的数据刷新。
更具体的代码实现请看源码:
源码下载
这篇关于自定义可拖拽的GridView的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!