一个可以拖动的自定义Gridview代码

这个可以拖动的gridview继承于gridview,所以,用法和gridview一样,

代码如下:

public class DragGridView extends GridView {

    private long dragResponseMS = 700; // item长按响应的时间
    private int mDragPosition;// 正在拖拽的position

    private boolean isDrag = false; // 是否可以拖拽,用于控件内部逻辑实现
    private boolean canDrag = true; // 是否可用拖拽,主要用于外部开放设置
    private boolean mAnimationEnd = true;

    private int mDownX;
    private int mDownY;
    private int moveX;
    private int moveY;

    private View mStartDragItemView = null; // 刚开始拖拽的item对应的View
    private ImageView mDragImageView; // 用于拖拽时显示的幻影镜像
    private Bitmap mDragBitmap; // 幻影镜像对应的Bitmap

    private final Vibrator mVibrator; // 震动器
    private final int mStatusHeight;// 状态栏的高度
    private final WindowManager mWindowManager;
    private WindowManager.LayoutParams mWindowLayoutParams; // item镜像的布局参数

    private int mPoint2ItemTop; // 按下的点到所在item的上边缘的距离
    private int mPoint2ItemLeft;
    private int mOffset2Top; // DragGridView距离屏幕顶部的偏移量
    private int mOffset2Left;

    private int mDownScrollBorder; // DragGridView自动向下滚动的边界值
    private int mUpScrollBorder; // DragGridView自动向上滚动的边界值

    private DragGridBaseAdapter mDragAdapter;
    private int mNumColumns;
    private int mColumnWidth;
    private boolean mNumColumnsSet;
    private int mHorizontalSpacing;

    private static final int speed = 20; // DragGridView自动滚动的速度
    private static final int MOVE_OFFSET = 25;
    private boolean moved = false;

    public static final int HANDLE_START = 0x3587;
    public static final int HANDLE_CANCLE = 0x3588;
    public static final int HANDLE_FINISH = 0x3589;
    private static OnMoveListener moveListener; // 拖拽开始与结束监听器
    private OnDeleteListener deleteListener; // 移动到垃圾桶时的监听器

    private float dragViewScale = 1.3f;

    private final TouchRect moveRect = new TouchRect();
    private final TouchRect gridRect = new TouchRect();
    private final TouchRect trashRect = new TouchRect();

    public DragGridView(Context context) {
        this(context, null);
    }

    public DragGridView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mVibrator = (Vibrator) context
                .getSystemService(Context.VIBRATOR_SERVICE);
        mWindowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        mStatusHeight = getStatusHeight(context); // 获取状态栏的高度

        if (!mNumColumnsSet) {
            mNumColumns = AUTO_FIT;
        }
    }

    /**
     * 获取状态栏的高度
     *
     * @param context
     * @return
     */
    private static int getStatusHeight(Context context) {
        int statusHeight = 0;
        Rect localRect = new Rect();

        ((Activity) context).getWindow().getDecorView()
                .getWindowVisibleDisplayFrame(localRect);
        statusHeight = localRect.top;
        if (0 == statusHeight) {
            Class<?> localClass;
            try {
                localClass = Class.forName("com.android.internal.R$dimen");
                Object localObject = localClass.newInstance();
                int i5 = Integer.parseInt(localClass
                        .getField("status_bar_height").get(localObject)
                        .toString());
                statusHeight = context.getResources().getDimensionPixelSize(i5);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return statusHeight;
    }

    private static final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            if (moveListener != null) {
                if (msg.what == HANDLE_START) {
                    moveListener.startMove();
                } else if (msg.what == HANDLE_FINISH) {
                    moveListener.finishMove();
                } else if (msg.what == HANDLE_CANCLE) {
                    moveListener.cancleMove();
                }
            }
        }

        ;
    };

    // 用来处理是否为长按的Runnable
    private final Runnable mLongClickRunnable = new Runnable() {
        @Override
        public void run() {
            if (!canDrag) {
                return;
            }
            isDrag = true; // 设置可以拖拽
            moved = true;
            mHandler.sendEmptyMessage(HANDLE_START);
            mVibrator.vibrate(50); // 震动一下

            mStartDragItemView.setVisibility(View.INVISIBLE);// 隐藏该item

            createDragImage(mDragBitmap, mDownX, mDownY);
            mDragBitmap = null;
        }
    };

    /**
     * 若设置为AUTO_FIT,计算有多少列
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mNumColumns == AUTO_FIT) {
            int numFittedColumns = 1; // 可用列

            if (mColumnWidth > 0) {
                int gridWidth = Math.max(MeasureSpec.getSize(widthMeasureSpec)
                        - getPaddingLeft() - getPaddingRight(), 0);
                numFittedColumns = gridWidth / mColumnWidth;
                if (numFittedColumns > 0) {
                    while (numFittedColumns != 1) {
                        if (numFittedColumns * mColumnWidth
                                + (numFittedColumns - 1) * mHorizontalSpacing > gridWidth) {
                            numFittedColumns--;
                        } else {
                            break;
                        }
                    }
                }
            } else {
                numFittedColumns = 2;
            }
            mNumColumns = numFittedColumns;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initRecord();
    }

    /**
     * 初始化坐标数据
     */
    private void initRecord() {
        gridRect.left = this.getLeft();
        gridRect.right = this.getRight();
        gridRect.top = this.getTop();
        gridRect.bottom = this.getBottom();
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);

        if (adapter instanceof DragGridBaseAdapter) {
            mDragAdapter = (DragGridBaseAdapter) adapter;
        } else {
            throw new IllegalStateException(
                    "the adapter must be implements DragGridAdapter");
        }
    }

    @Override
    public void setNumColumns(int numColumns) {
        super.setNumColumns(numColumns);
        mNumColumnsSet = true;
        this.mNumColumns = numColumns;
    }

    @Override
    public void setColumnWidth(int columnWidth) {
        super.setColumnWidth(columnWidth);
        mColumnWidth = columnWidth;
    }

    @Override
    public void setHorizontalSpacing(int horizontalSpacing) {
        super.setHorizontalSpacing(horizontalSpacing);
        this.mHorizontalSpacing = horizontalSpacing;
    }

    /**
     * 设置响应拖拽的毫秒数,默认是700毫秒
     * @param dragResponseMS
     */
    public void setDragResponseMS(long dragResponseMS) {
        this.dragResponseMS = dragResponseMS;
    }

    public void setOnDeleteListener(OnDeleteListener l) {
        this.deleteListener = l;
    }

    public void setDragEnable(boolean isDrag) {
        this.canDrag = isDrag;
        if (canDrag) {
            mHandler.removeCallbacks(mLongClickRunnable);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (canDrag) {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mDownX = (int) ev.getX();
                    mDownY = (int) ev.getY();
                    moveRect.left = mDownX - MOVE_OFFSET;
                    moveRect.right = mDownX + MOVE_OFFSET;
                    moveRect.top = mDownY - MOVE_OFFSET;
                    moveRect.bottom = mDownY + MOVE_OFFSET;

                    // 根据按下的X,Y坐标获取所点击item的position
                    mDragPosition = pointToPosition(mDownX, mDownY);

                    if (mDragPosition == AdapterView.INVALID_POSITION || mDragPosition == getDragAdapter().getRealCount()) {
                        return super.dispatchTouchEvent(ev);
                    }

                    // 使用Handler延迟dragResponseMS执行mLongClickRunnable
                    mHandler.postDelayed(mLongClickRunnable, dragResponseMS);

                    // 根据position获取该item所对应的View
                    mStartDragItemView = getChildAt(mDragPosition
                            - getFirstVisiblePosition());

                    mPoint2ItemTop = mDownY - mStartDragItemView.getTop();
                    mPoint2ItemLeft = mDownX - mStartDragItemView.getLeft();

                    mOffset2Top = (int) (ev.getRawY() - mDownY);
                    mOffset2Left = (int) (ev.getRawX() - mDownX);

                    // 获取DragGridView自动向上滚动的偏移量,小于这个值,DragGridView向下滚动
                    mDownScrollBorder = getHeight() / 5;
                    // 大于这个值,DragGridView向上滚动
                    mUpScrollBorder = getHeight() * 4 / 5;

                    // 开启mDragItemView绘图缓存
                    mStartDragItemView.setDrawingCacheEnabled(true);
                    // 获取mDragItemView在缓存中的Bitmap对象
                    mDragBitmap = Bitmap.createBitmap(mStartDragItemView
                            .getDrawingCache());
                    // 这一步很关键,释放绘图缓存,避免出现重复的镜像
                    mStartDragItemView.destroyDrawingCache();
                    break;
                case MotionEvent.ACTION_MOVE:
                    // 如果我们在按下的item上面移动,只要不超过item的边界我们就不移除mRunnable
                    if (!isTouchInItem(moveRect, ev.getX(), ev.getY())) {
                        mHandler.removeCallbacks(mLongClickRunnable);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    mHandler.removeCallbacks(mScrollRunnable);
                    mHandler.removeCallbacks(mLongClickRunnable);
                    if (moved && getAdapter().getCount() > 0) {
                        mHandler.sendEmptyMessage(HANDLE_FINISH);
                    } else {
                        mHandler.sendEmptyMessage(HANDLE_CANCLE);
                    }
                    moved = false;
                    break;
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (isDrag && canDrag && mDragImageView != null) {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initRecord();
                    break;
                case MotionEvent.ACTION_MOVE:
                    moveX = (int) ev.getX();
                    moveY = (int) ev.getY();

                    onDragItem(moveX, moveY);// 拖动item
                    break;
                case MotionEvent.ACTION_UP:
                    onStopDrag();
                    isDrag = false;
                    break;
            }
            return true;
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 是否点击在GridView的item上面
     *
     * @param moveRect
     * @param x
     * @param y
     * @return
     */
    private boolean isTouchInItem(TouchRect moveRect, float x, float y) {
        // 防止手抖的处理,如果是横向在item上移动是没有影响的,
        // 但是纵向由于外层上下拉控件还是会有影响,具体解决请看NoteBookFragment类中的mSwipeRefreshLayout.setOnTouchListener方法
        if (x < moveRect.right && x > moveRect.left && y < moveRect.bottom
                && y > moveRect.top) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 创建拖动的镜像
     *
     * @param bitmap
     * @param downX  按下的点相对父控件的X坐标
     * @param downY  按下的点相对父控件的X坐标
     */
    private void createDragImage(Bitmap bitmap, int downX, int downY) {
        mWindowLayoutParams = new WindowManager.LayoutParams();
        mWindowLayoutParams.format = PixelFormat.TRANSLUCENT; // 图片之外的其他地方透明

        mWindowLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
        initDragOffset(bitmap);
        mWindowLayoutParams.x = downX - mPoint2ItemLeft + mOffset2Left;
        mWindowLayoutParams.y = downY - mPoint2ItemTop + mOffset2Top - mStatusHeight;
        mWindowLayoutParams.alpha = 0.618f; // 透明度
        mWindowLayoutParams.width = (int) (bitmap.getWidth() * dragViewScale);
        mWindowLayoutParams.height = (int) (bitmap.getHeight() * dragViewScale);
        mWindowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;

        mDragImageView = new ImageView(getContext());
        mDragImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        mDragImageView.setImageBitmap(bitmap);
        mWindowManager.addView(mDragImageView, mWindowLayoutParams);
    }

    private void initDragOffset(Bitmap bitmap){
        mOffset2Left = mOffset2Left - (int) (bitmap.getWidth() * (dragViewScale - 1) / 2);
        mOffset2Top = mOffset2Top - (int) (bitmap.getHeight() * (dragViewScale - 1) / 2);
    }

    /**
     * 从界面上面移动拖动镜像
     */
    private void removeDragImage() {
        if (mDragImageView != null) {
            mWindowManager.removeView(mDragImageView);
            mDragImageView = null;
        }
    }

    /**
     * 拖动item,在里面实现了item镜像的位置更新,item的相互交换以及GridView的自行滚动
     *
     * @param moveX
     * @param moveY
     */
    private void onDragItem(int moveX, int moveY) {
        mWindowLayoutParams.x = moveX - mPoint2ItemLeft + mOffset2Left;
        mWindowLayoutParams.y = moveY - mPoint2ItemTop + mOffset2Top
                - mStatusHeight;
        mWindowManager.updateViewLayout(mDragImageView, mWindowLayoutParams); // 更新镜像的位置
        onSwapItem(moveX, moveY);
        // GridView自动滚动
        mHandler.post(mScrollRunnable);
    }

    /**
     * 当moveY的值大于向上滚动的边界值,触发GridView自动向上滚动 当moveY的值小于向下滚动的边界值,触发GridView自动向下滚动
     * 否则不进行滚动
     */
    private final Runnable mScrollRunnable = new Runnable() {

        @Override
        public void run() {
            int scrollY;
            if (getFirstVisiblePosition() == 0
                    || getLastVisiblePosition() == getCount() - 1) {
                mHandler.removeCallbacks(mScrollRunnable);
            }

            if (moveY > mUpScrollBorder) {
                scrollY = speed;
                mHandler.postDelayed(mScrollRunnable, 25);
            } else if (moveY < mDownScrollBorder) {
                scrollY = -speed;
                mHandler.postDelayed(mScrollRunnable, 25);
            } else {
                scrollY = 0;
                mHandler.removeCallbacks(mScrollRunnable);
            }

            smoothScrollBy(scrollY, 10);
        }
    };

    /**
     * 交换item,并且控制item之间的显示与隐藏效果
     *
     * @param moveX
     * @param moveY
     */
    private void onSwapItem(int moveX, int moveY) {
        // 获取我们手指移动到的那个item的position

        final int tempPosition = pointToPosition(moveX, moveY);

        // 假如tempPosition 改变了并且tempPosition不等于-1,则进行交换

        if (tempPosition != mDragPosition
                && tempPosition != AdapterView.INVALID_POSITION
                && tempPosition != getDragAdapter().getRealCount()
                && mAnimationEnd) {
            mDragAdapter.reorderItems(mDragPosition, tempPosition);
            mDragAdapter.setHideItem(tempPosition);

            final ViewTreeObserver observer = getViewTreeObserver();
            observer.addOnPreDrawListener(new OnPreDrawListener() {

                @Override
                public boolean onPreDraw() {
                    observer.removeOnPreDrawListener(this);
                    animateReorder(mDragPosition, tempPosition);
                    mDragPosition = tempPosition;
                    return true;
                }
            });

        }
    }

    /**
     * 创建移动动画
     *
     * @param view
     * @param startX
     * @param endX
     * @param startY
     * @param endY
     * @return
     */
    private AnimatorSet createTranslationAnimations(View view, float startX,
                                                    float endX, float startY, float endY) {
        ObjectAnimator animX = ObjectAnimator.ofFloat(view, "translationX",
                startX, endX);
        ObjectAnimator animY = ObjectAnimator.ofFloat(view, "translationY",
                startY, endY);
        AnimatorSet animSetXY = new AnimatorSet();
        animSetXY.playTogether(animX, animY);
        return animSetXY;
    }

    /**
     * item的交换动画效果
     *
     * @param oldPosition
     * @param newPosition
     */
    private void animateReorder(final int oldPosition, final int newPosition) {
        boolean isForward = newPosition > oldPosition;
        List<Animator> resultList = new LinkedList<Animator>();
        if (isForward) {
            for (int pos = oldPosition; pos < newPosition; pos++) {
                View view = getChildAt(pos - getFirstVisiblePosition());
                if (view == null) {
                    continue;
                }
                if ((pos + 1) % mNumColumns == 0) {
                    resultList.add(createTranslationAnimations(view,
                            -view.getWidth() * (mNumColumns - 1), 0,
                            view.getHeight(), 0));
                } else {
                    resultList.add(createTranslationAnimations(view,
                            view.getWidth(), 0, 0, 0));
                }
            }
        } else {
            for (int pos = oldPosition; pos > newPosition; pos--) {
                View view = getChildAt(pos - getFirstVisiblePosition());
                if ((pos + mNumColumns) % mNumColumns == 0) {
                    resultList.add(createTranslationAnimations(view,
                            view.getWidth() * (mNumColumns - 1), 0,
                            -view.getHeight(), 0));
                } else {
                    resultList.add(createTranslationAnimations(view,
                            -view.getWidth(), 0, 0, 0));
                }
            }
        }

        AnimatorSet resultSet = new AnimatorSet();
        resultSet.playTogether(resultList);
        resultSet.setDuration(300);
        resultSet.setInterpolator(new AccelerateDecelerateInterpolator());
        resultSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                mAnimationEnd = false;
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mAnimationEnd = true;
            }
        });
        resultSet.start();
    }

    /**
     * 停止拖拽我们将之前隐藏的item显示出来,并将镜像移除
     */
    private void onStopDrag() {
        View view = getChildAt(mDragPosition - getFirstVisiblePosition());
        if (view != null) {
            view.setVisibility(View.VISIBLE);
        }
        mDragAdapter.setHideItem(-1);
        removeDragImage();
    }

    private DragGridBaseAdapter getDragAdapter() {
        return (DragGridBaseAdapter) getAdapter();
    }

    public void setOnMoveListener(OnMoveListener listener) {
        moveListener = listener;
    }

    public interface OnMoveListener {
        void startMove();

        void finishMove();

        void cancleMove();
    }

    public interface OnDeleteListener {
        void onDelete(int position);
    }

    public interface DragGridBaseAdapter {
        /**
         * 移动时回调
         */
        void reorderItems(int oldPosition, int newPosition);

        /**
         * 隐藏时回调
         */
        void setHideItem(int hidePosition);

        int getRealCount();
    }

    private class TouchRect {
        int top;
        int bottom;
        int left;
        int right;
    }
}
时间: 2024-11-05 18:12:53

一个可以拖动的自定义Gridview代码的相关文章

Android学习笔记-构建一个可复用的自定义BaseAdapter

转载自http://www.runoob.com/w3cnote/android-tutorial-customer-baseadapter.html   作者:coder-pig 本节引言: 如题,本节给大家带来的是构建一个可复用的自定义BaseAdapter,我们每每涉及到ListView GridView等其他的Adapter控件,都需要自己另外写一个BaseAdapter类,这样显得非常麻烦, 又比如,我们想在一个界面显示两个ListView的话,我们也是需要些两个BaseAdapter

PullToRefreshScrollView实现顶层搜索框 滑动可隐藏 下面刷自定义GridView

最近项目里有个需求,要求用GridView去显示商品,滑动到底部下拉能加载更多商品,向下滑动过程中需要隐藏掉自定义的Action(搜索框)布局,向上滑动能显示出来,整体效果类似淘宝APP搜索结果页那样. 起初觉得挺简单的,但是后来才发现还是得转一点脑子.最开始我想用PullToRefreshGridView,但是后来发现GridView没有添加headview的方法,只能采用PullToRefreshScrollView内嵌套GridView的方法,Scrollview里多放一个空白布局当Gri

可拖拽GridView代码解析

本片学习笔记是对eoe网上一个项目代码的解读,具体项目作者的博客如下:http://blog.csdn.net/vipzjyno1/article/details/26514543.项目源码下载地址为https://github.com/Rano1/TopNews本篇只对可拖拽的GridView的代码进行解读,同时修改了原项目中不必要的变量.去掉了不必要或者逻辑错误的代码,也删除了方法中不必要的局部变量和计算.通过对这个读这个代码,自己也着实学到了不少的东西(毕竟自己刚接触android不久,特

Android基础入门教程——2.4.7 构建一个可复用的自定义BaseAdapter

Android基础入门教程--2.4.7 构建一个可复用的自定义BaseAdapter 标签(空格分隔): Android基础入门教程 本节引言: 如题,本节给大家带来的是构建一个可复用的自定义BaseAdapter,我们每每涉及到ListView GridView等其他的Adapter控件,都需要自己另外写一个BaseAdapter类,这样显得非常麻烦, 又比如,我们想在一个界面显示两个ListView的话,我们也是需要些两个BaseAdapter- 这,程序员都是喜欢偷懒的哈,这节我们就来写

Xamarin.Forms自定义GridView

Xamarin.Forms自定义GridView 在开发中,我们经常用到以格子的形式来展示我们的数据,在很多平台的控件中我们叫做GridView, 在Xamarin.Forms中没有原生的GridView,这里简单介绍一种利用Xamarin.Forms中的Grid来实现 GridView的方法. 原理就是对Grid动态添加RowDefinition和ColumnDefinition. 代码如下: 1 using System; 2 using System.Collections; 3 usin

关于UITabBar各部分自定义的代码片段

一.自定义TabBar选项卡背景默认UITabBarController的TabBar背景是黑色的,如何自定义成背景图片呢? UITabBarController *tabBarController = [[UITabBarController alloc] init]; // 获取选项卡控制器视图的所有子视图,保存到一数组中 NSArray *array = [tabBarController.view subviews]; // 索引值为1的应该就是TabBar UITabBar  *tab

Android开发:自定义GridView/ListView数据源

http://mobile.51cto.com/android-259861.htm 在开发中,我们常常会遇到比较复杂的GridView/ListView的布局,重新实现BaseAdapter不但能帮助我们实现我们想要的布局效果,并且在绑定大数据量时也不会感觉有卡壳现象.记得以前用一个ListView直接去绑定手机内的联系人Cursor(一百多号人),滑动的时候就会有卡的感觉.今天决定写个Demo是因为在项目中可能会要实现这样的一个效果:一个GridView中绑定4个ImageButton,有些

一个简单的jsp自定义标签

学到了一个简单的jsp自定义标签,后面有更多的例子,会更新出来: 例子1: 步骤: 1.编写标签实现类: 继承javax.servlet.jsp.tagext.SimpleTagSupport; 重写doTag,实现在网页上输出: 2.在web-inf目录或其子目录下,建立helloword.tld文件,即自定义标签的说明文件 注意:标签处理类必须放在包中,不能是裸体类:不需要修改web.xml: //tld: tag lib description 标签库描述 java代码: package

sublime自定义snippet代码片段

相信很多人喜欢sublime编辑工具有两个原因:第一sublime很轻巧方便:第二sublime提供很多自定义拓展功能,包括很简单且和很好用的代码片段功能snippet文件. 今天,在这里就介绍下sublime如何自定义各种代码片段,先来感受一下效果: 上面看到的是我用sublime初始化一个html文件的操作gif,相信这是很多人在新项目必经的步骤.而我里面不同的时,不需要任何复制,只需几秒钟就完成整个初始化的工作,这就是sublime的魅力之一:代码片段snippet. 下面我就拿上面的动画