Android自定义View实现可以拖拽的GridView

Android自定义View实现可以拖拽的GridView,第1张

概述先看看效果图主要思想:1、监听触碰事件2、用WindowManager添加拖曳的图片3、用Collections.swap()交换List数据

先看看效果图

主要思想:

1、监听触碰事件
2、用WindowManager添加拖曳的图片
3、用Collections.swap()交换List数据

自定义代码:

public class DragGrIDVeiw extends GrIDVIEw {  private final int PRESS_TIME = 1000;//长按时间  private int mDownX;//触碰时的X坐标  private int mDownY;//触碰时的Y坐标  private int mMoveX;//移动时的X坐标  private int mMoveY;//移动时的Y坐标  private int mOffset2top;//DragGrIDVIEw距离屏幕顶部的偏移量  private int mOffset2left;//DragGrIDVIEw距离屏幕左边的偏移量  private int mPointToItemtop;//触碰点距离ItemVIEw的上边距  private int mPointToItemleft;//触碰点距离ItemVIEw的左边距  private int mStatusHeight;//状态栏高度  private boolean isDraging;//是否正在拖曳  private Bitmap mBitmap;//ItemVIEw的图片  private int mtouchPostiion;//触碰的位置  private VIEw mtouchItemVIEw;//触碰的ItemVIEw  private Vibrator mVibrator;//震动器  private ImageVIEw mDragImageVIEw;//拖曳的VIEw  private WindowManager mWindowManager;//窗口管理器  private WindowManager.LayoutParams mWindowLayoutParams;//窗口管理器布局  private OnChanageListener onChanageListener;//交换事件监听器  private Handler mHandler = new Handler();  public DragGrIDVeiw(Context context) {    this(context,null);  }  public DragGrIDVeiw(Context context,AttributeSet attrs) {    this(context,attrs,0);  }  public DragGrIDVeiw(Context context,AttributeSet attrs,int defStyleAttr) {    super(context,defStyleAttr);    mStatusHeight = getStatusHeight(context);    mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);    mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);  }  @OverrIDe  public boolean dispatchtouchEvent(MotionEvent ev) {    switch (ev.getAction()) {      case MotionEvent.ACTION_DOWN:        //使用Handler延迟dragResponseMS执行mLongClickRunnable        mHandler.postDelayed(mLongClickRunnable,PRESS_TIME);        mDownX = (int) ev.getX();        mDownY = (int) ev.getY();        //根据按下的X,Y坐标获取所点击item的position        mtouchPostiion = pointToposition(mDownX,mDownY);        if (mtouchPostiion == AdapterVIEw.INVALID_position) {          return super.dispatchtouchEvent(ev);        }        //根据position获取该item所对应的VIEw        mtouchItemVIEw = getChildAt(mtouchPostiion - getFirstVisibleposition());        //下面这几个距离大家可以参考我的博客上面的图来理解下        mPointToItemtop = mDownY - mtouchItemVIEw.gettop();        mPointToItemleft = mDownX - mtouchItemVIEw.getleft();        mOffset2top = (int) (ev.getRawY() - mDownY);        mOffset2left = (int) (ev.getRawX() - mDownX);        //开启mDragItemVIEw绘图缓存        mtouchItemVIEw.setDrawingCacheEnabled(true);        //获取mDragItemVIEw在缓存中的Bitmap对象        mBitmap = Bitmap.createBitmap(mtouchItemVIEw.getDrawingCache());        //这一步很关键,释放绘图缓存,避免出现重复的镜像        mtouchItemVIEw.destroyDrawingCache();        break;      case MotionEvent.ACTION_MOVE:        int moveX = (int) ev.getX();        int moveY = (int) ev.getY();        //拖曳点超出GrIDVIEw区域则取消拖曳事件        if (ev.getY() > getHeight() || ev.getY() < 0) {          onStopDrag();        }        //如果我们在按下的item上面移动,只要超过item的边界就移除mRunnable        if (!istouchInItem(mtouchItemVIEw,moveX,moveY)) {          mHandler.removeCallbacks(mLongClickRunnable);        }        break;      case MotionEvent.ACTION_UP:        mHandler.removeCallbacks(mLongClickRunnable);        break;    }    return super.dispatchtouchEvent(ev);  }  @OverrIDe  public boolean ontouchEvent(MotionEvent ev) {    if (isDraging && mDragImageVIEw != null) {      switch (ev.getAction()) {        case MotionEvent.ACTION_MOVE:          mMoveX = (int) ev.getX();          mMoveY = (int) ev.getY();          //拖动item          onDragItem(mMoveX,mMoveY);          break;        case MotionEvent.ACTION_UP:          onStopDrag();          break;      }      return true;    }    return super.ontouchEvent(ev);  }  //处理长按事件的线程  private Runnable mLongClickRunnable = new Runnable() {    @OverrIDe    public voID run() {      isDraging = true; //设置可以拖拽      mVibrator.vibrate(50); //震动一下      mtouchItemVIEw.setVisibility(VIEw.INVISIBLE);//隐藏该ItemVIEw      //根据我们按下的点显示ItemVIEw镜像      createDragVIEw(mBitmap,mDownX,mDownY);    }  };  //添加拖动VIEw  private voID createDragVIEw(Bitmap bitmap,int downX,int downY) {    mWindowLayoutParams = new WindowManager.LayoutParams();    mWindowLayoutParams.format = PixelFormat.TRANSLUCENT; //图片之外的其他地方透明    mWindowLayoutParams.gravity = Gravity.top | Gravity.left;    mWindowLayoutParams.x = downX - mPointToItemtop + mOffset2left;    mWindowLayoutParams.y = downY - mPointToItemtop + mOffset2top - mStatusHeight;    mWindowLayoutParams.Alpha = 0.6f; //透明度    mWindowLayoutParams.wIDth = WindowManager.LayoutParams.WRAP_CONTENT;    mWindowLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;    mWindowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE        | WindowManager.LayoutParams.FLAG_NOT_touchABLE;    mDragImageVIEw = new ImageVIEw(getContext());    mDragImageVIEw.setimageBitmap(bitmap);    mWindowManager.addVIEw(mDragImageVIEw,mWindowLayoutParams);  }  private voID removeDragVIEw() {    if (mDragImageVIEw != null) {      mWindowManager.removeVIEw(mDragImageVIEw);      mDragImageVIEw = null;    }  }  //是否点击在GrIDVIEw的item上面  private boolean istouchInItem(VIEw dragVIEw,int x,int y) {    int leftOffset = dragVIEw.getleft();    int topOffset = dragVIEw.gettop();    if (x < leftOffset || x > leftOffset + dragVIEw.getWIDth()) {      return false;    }    if (y < topOffset || y > topOffset + dragVIEw.getHeight()) {      return false;    }    return true;  }  //拖动事件处理  private voID onDragItem(int moveX,int moveY) {    mWindowLayoutParams.x = moveX - mPointToItemleft + mOffset2left;    mWindowLayoutParams.y = moveY - mPointToItemtop + mOffset2top - mStatusHeight;    mWindowManager.updateVIEwLayout(mDragImageVIEw,mWindowLayoutParams); //更新DragVIEw的位置    onSwAPItem(moveX,moveY);//Item的相互交换  }  //交换item,并且控制item之间的显示与隐藏效果  private voID onSwAPItem(int moveX,int moveY) {    //获取我们手指移动到的那个item的position    int tempposition = pointToposition(moveX,moveY);    //假如tempposition 改变了并且tempposition不等于-1,则进行交换    if (tempposition != mtouchPostiion && tempposition != AdapterVIEw.INVALID_position) {      getChildAt(tempposition - getFirstVisibleposition()).setVisibility(VIEw.INVISIBLE);//拖动到了新的item,新的item隐藏掉      getChildAt(mtouchPostiion - getFirstVisibleposition()).setVisibility(VIEw.VISIBLE);//之前的item显示出来      if (onChanageListener != null) {        onChanageListener.onChange(mtouchPostiion,tempposition);      }      mtouchPostiion = tempposition;    }  }  //停止拖拽我们将之前隐藏的item显示出来,并将DragVIEw移除  private voID onStopDrag() {    isDraging = false;    getChildAt(mtouchPostiion - getFirstVisibleposition()).setVisibility(VIEw.VISIBLE);    removeDragVIEw();  }  //Item交换事件监听  public voID setonchangelistener(OnChanageListener onChanageListener) {    this.onChanageListener = onChanageListener;  }  //获取状态栏高度  private 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;  }  //当item交换位置的时候回调的方法,我们只需要在该方法中实现数据的交换即可  public interface OnChanageListener {    public voID onChange(int from,int to);  }}

使用方法:

   List<HashMap<String,Object>> dataSourceList = new ArrayList<>();    dragVeiw = (DragGrIDVeiw) findVIEwByID(R.ID.vIEw_drag);    for (int i = 0; i < 8; i++) {      HashMap<String,Object> itemHashMap = new HashMap<>();      itemHashMap.put("item_image",R.drawable.sample_1);      itemHashMap.put("item_text","拖拽 " + Integer.toString(i));      dataSourceList.add(itemHashMap);    }    final SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,dataSourceList,R.layout.item_drag,new String[]{"item_image","item_text"},new int[]{R.ID.item_image,R.ID.item_text});    dragVeiw.setAdapter(mSimpleAdapter);    dragVeiw.setonchangelistener(new DragGrIDVeiw.OnChanageListener() {      @OverrIDe      public voID onChange(int from,int to) {        HashMap<String,Object> temp = dataSourceList.get(from);        //这里的处理需要注意下        if (from < to) {          for (int i = from; i < to; i++) {            Collections.swap(dataSourceList,i,i + 1);          }        } else if (from > to) {          for (int i = from; i > to; i--) {            Collections.swap(dataSourceList,i - 1);          }        }        dataSourceList.set(to,temp);        mSimpleAdapter.notifyDataSetChanged();      }    });

附录:

Log.v("-->getWIDth",String.valueOf(getWIDth()));//DragVIEw的宽度Log.v("-->getHeight",String.valueOf(getHeight()));//DragVIEw的高度Log.v("-->getleft",String.valueOf(getleft()));//DragVIEw左边距离屏幕左侧的长度Log.v("-->gettop",String.valueOf(gettop()));///DragVIEw上边距离屏幕顶部的长度Log.v("-->getRawX",String.valueOf(ev.getRawX()));//触碰点相对于屏幕的X坐标Log.v("-->getRawY",String.valueOf(ev.getRawY()));//触碰点相对于屏幕的Y坐标Log.v("-->getX",String.valueOf(ev.getX()));//触碰点相对于DragVIEw的X坐标Log.v("-->getY",String.valueOf(ev.getY()));//触碰点相对于DragVIEw的Y坐标Log.v("-->getItemWIDth",String.valueOf(mtouchItemVIEw.getWIDth()));//DragVIEw中ItemVIEw的宽度Log.v("-->getItemHeight",String.valueOf(mtouchItemVIEw.getHeight()));//DragVIEw中ItemVIEw的高度Log.v("-->getItemleft",String.valueOf(mtouchItemVIEw.getleft()));//DragVIEw中ItemVIEw左边距离DragVIEw左侧的长度Log.v("-->getItemtop",String.valueOf(mtouchItemVIEw.gettop()));//DragVIEw中ItemVIEw上边距离DragVIEw顶部的长度

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android自定义View实现可以拖拽的GridView全部内容,希望文章能够帮你解决Android自定义View实现可以拖拽的GridView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1141926.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存