Android自定义gridView仿头条频道拖动管理功能

Android自定义gridView仿头条频道拖动管理功能,第1张

概述Android自定义gridView仿头条频道拖动管理功能 项目中遇到这样个需求:app的功能导航需要可拖动排序,类似头条中的频道拖动管理.效果如下,gif不是很顺畅,真机会好很多. 虽然类似的文章网上搜一下有很多,但写的都不令人满意,注释不清晰,而且动画还不够流畅.经本人整理优化后,拿出来供后续有需要的使用. 实现原理: gridView作为基本控件 WindowManager.addView的方式实现可拖动的view TranslateAnimation实现移动动画,动画完后更新adapter即可 主要的实现原理上面已经说明,源码中关键的地点也有注释

项目中遇到这样个需求:app的功能导航需要可拖动排序,类似头条中的频道拖动管理。效果如下,gif不是很顺畅,真机会好很多。

虽然类似的文章网上搜一下有很多,但写的都不令人满意,注释不清晰,而且动画还不够流畅。经本人整理优化后,拿出来供后续有需要的使用。

实现原理:

grIDVIEw作为基本控件WindowManager.addVIEw的方式实现可拖动的vIEwTranslateAnimation实现移动动画,动画完后更新adapter即可

主要的实现原理上面已经说明,源码中关键的地点也有注释,因此下面直接上源码。

package com.hai.draggrID;import androID.content.Context;import androID.graphics.Bitmap;import androID.graphics.PixelFormat;import androID.util.AttributeSet;import androID.vIEw.Gravity;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.WindowManager;import androID.vIEw.animation.Animation;import androID.vIEw.animation.TranslateAnimation;import androID.Widget.AdapterVIEw;import androID.Widget.BaseAdapter;import androID.Widget.GrIDVIEw;import androID.Widget.ImageVIEw;/** * 长按拖动图标可以调整item位置的GrIDvIEw * Created by huanghp on 2019/10/15. * Email h1132760021@sina.com */public class DragGrIDVIEw extends GrIDVIEw { private static final String TAG = "DragGrIDVIEw"; private int downX,downY; private int rawX,rawY; private int lastposition = INVALID_position; private int vIEwL,vIEwT; private int itemHeight,itemWIDth; private int itemCount; private double dragScale = 1.2D;//拖动vIEw的放大比例 private ImageVIEw dragImageVIEw; private WindowManager windowManager = null; private WindowManager.LayoutParams windowParams = null; private boolean isMoving = false; private Animation lastAnimation; private static final long TIME_ANIMATE = 300; public DragGrIDVIEw(Context context,AttributeSet attrs) {  this(context,attrs,0); } public DragGrIDVIEw(Context context,AttributeSet attrs,int defStyleAttr) {  super(context,defStyleAttr);  setonItemLongClickListener(new OnItemLongClickListener() {   @OverrIDe   public boolean onItemLongClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) {    lastposition = position;    VIEw dragVIEw = getChildAt(lastposition - getFirstVisibleposition());    itemHeight = dragVIEw.getHeight();    itemWIDth = dragVIEw.getWIDth();    itemCount = getCount();    int rows = itemCount / getNumColumns();// 算出行数    int left = (itemCount % getNumColumns());// 算出最后一行多余的数量    if (lastposition != INVALID_position) {     vIEwL = downX - dragVIEw.getleft();     vIEwT = downY - dragVIEw.gettop();     dragVIEw.destroyDrawingCache();     dragVIEw.setDrawingCacheEnabled(true);     Bitmap bitmap = Bitmap.createBitmap(dragVIEw.getDrawingCache());     startDrag(bitmap);     dragVIEw.setVisibility(INVISIBLE);     isMoving = false;     ((Adapter) getAdapter()).setIsDrag(true);     requestdisallowIntercepttouchEvent(true);     return true;    }    return false;   }  }); } private voID startDrag(Bitmap dragBitmap) {  stopDrag();  windowParams = new WindowManager.LayoutParams();  windowParams.gravity = Gravity.top | Gravity.left;  //得到prevIEw左上角相对于屏幕的坐标  windowParams.x = rawX - vIEwL;  windowParams.y = rawY - vIEwT;  //设置拖拽item的宽和高  windowParams.wIDth = (int) (dragScale * dragBitmap.getWIDth());// 放大dragScale倍,可以设置拖动后的倍数  windowParams.height = (int) (dragScale * dragBitmap.getHeight());// 放大dragScale倍,可以设置拖动后的倍数  this.windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE    | WindowManager.LayoutParams.FLAG_NOT_touchABLE    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;  this.windowParams.format = PixelFormat.TRANSLUCENT;  this.windowParams.windowAnimations = 0;  ImageVIEw iv = new ImageVIEw(getContext());  iv.setimageBitmap(dragBitmap);  windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);  windowManager.addVIEw(iv,windowParams);  dragImageVIEw = iv; } private voID stopDrag() {  if (dragImageVIEw != null && windowManager != null) {   windowManager.removeVIEw(dragImageVIEw);   dragImageVIEw = null;  } } @OverrIDe public boolean ontouchEvent(MotionEvent ev) {  int x = (int) ev.getX();  int y = (int) ev.getY();  switch (ev.getAction()) {   case MotionEvent.ACTION_DOWN:    downX = x;    downY = y;    rawX = (int) ev.getRawX();    rawY = (int) ev.getRawY();    break;   case MotionEvent.ACTION_MOVE:    if (dragImageVIEw != null && lastposition != INVALID_position) {     updateDrag((int) ev.getRawX(),(int) ev.getRawY());     if (!isMoving) onMove(x,y,false);    }    break;   case MotionEvent.ACTION_UP://    Log.e(TAG,"dragImageVIEw is null=" + (dragImageVIEw == null) + ",lastposition=" + lastposition//      + ",pointToposition=" + pointToposition(x,y) + ",ismove=" + isMoving);    if (dragImageVIEw != null && lastposition != INVALID_position) {//     if (isMoving) onMove(x,true);//动画还未执行完的情况下,重设动画会清除之前设置的动画。     stopDrag();     ((Adapter) getAdapter()).setIsDrag(false);     ((BaseAdapter) getAdapter()).notifyDataSetChanged();     requestdisallowIntercepttouchEvent(false);    }    break;  }  return super.ontouchEvent(ev); } private voID onMove(int moveX,int moveY,boolean isMoveUp) {  final int targetposition = pointToposition(moveX,moveY);  if (targetposition != INVALID_position) {   if (targetposition == lastposition) {    //移动位置在还未到新item内    return;   }   //移需要移动的动ITEM数量   int moveCount = targetposition - lastposition;   if (moveCount != 0) {    if (isMoveUp) {//手指抬起时,不执行动画直接交换数据     Adapter adapter = (Adapter) getAdapter();     adapter.exchange(lastposition,targetposition);     lastposition = targetposition;     isMoving = false;    } else {     int moveCountAbs = Math.abs(moveCount);     float toXvalue = 0,toYvalue = 0;     //moveXP移动的距离百分比(相对于自己长度的百分比)     float moveXP = ((float) getHorizontalSpacing() / (float) itemWIDth) + 1.0f;     float moveYP = ((float) getVerticalSpacing() / (float) itemHeight) + 1.0f;     int holdposition;//     Log.d(TAG,"start annimation=" + moveCountAbs);     for (int i = 0; i < moveCountAbs; i++) {      //从左往右,或是从上往下      if (moveCount > 0) {       holdposition = lastposition + i + 1;       //同一行       if (lastposition / getNumColumns() == holdposition / getNumColumns()) {        toXvalue = -moveXP;        toYvalue = 0;       } else if (holdposition % getNumColumns() == 0) {        toXvalue = (getNumColumns() - 1) * moveXP;        toYvalue = -moveYP;       } else {        toXvalue = -moveXP;        toYvalue = 0;       }      } else {       //从右往左,或是从下往上       holdposition = lastposition - i - 1;       if (lastposition / getNumColumns() == holdposition / getNumColumns()) {        toXvalue = moveXP;        toYvalue = 0;       } else if ((holdposition + 1) % getNumColumns() == 0) {        toXvalue = -(getNumColumns() - 1) * moveXP;        toYvalue = moveYP;       } else {        toXvalue = moveXP;        toYvalue = 0;       }      }      VIEw holdVIEw = getChildAt(holdposition);      Animation moveAnimation = createAnimation(toXvalue,toYvalue);      moveAnimation.setAnimationListener(new Animation.AnimationListener() {       @OverrIDe       public voID onAnimationStart(Animation animation) {        isMoving = true;       }       @OverrIDe       public voID onAnimationRepeat(Animation animation) {       }       @OverrIDe       public voID onAnimationEnd(Animation animation) {        // 如果为最后个动画结束,那执行下面的方法        if (animation == lastAnimation) {         Adapter adapter = (Adapter) getAdapter();         adapter.exchange(lastposition,targetposition);         lastposition = targetposition;         isMoving = false;        }       }      });      holdVIEw.startAnimation(moveAnimation);      if (holdposition == targetposition) {       lastAnimation = moveAnimation;      }     }    }   }  } } public Animation createAnimation(float toXValue,float toYValue) {  TranslateAnimation mTranslateAnimation = new TranslateAnimation(    Animation.relative_TO_SELF,0.0F,Animation.relative_TO_SELF,toXValue,toYValue);  mTranslateAnimation.setFillAfter(true);// 设置一个动画效果执行完毕后,VIEw对象保留在终止的位置。  mTranslateAnimation.setDuration(TIME_ANIMATE);  return mTranslateAnimation; } private voID updateDrag(int rawX,int rawY) {  windowParams.Alpha = 0.6f;  windowParams.x = rawX - vIEwL;  windowParams.y = rawY - vIEwT;  windowManager.updateVIEwLayout(dragImageVIEw,windowParams); } static abstract class Adapter extends BaseAdapter {  protected boolean isDrag;  protected int holdposition = -1;  public voID setIsDrag(boolean isDrag) {   this.isDrag = isDrag;  }  public voID exchange(int startposition,int endPositon) {   holdposition = endPositon;  } }}

主要的代码就是DragGrIDVIEw,拿到此vIEw实现起来就相当简单了。为了文章完整性,下面也贴上本效果图的主要使用代码。

String[] items = new String[]{"头条","视频","娱乐","体育","北京","新时代","网易号","段子","冰雪运动","科技","汽车","轻松一刻","时尚","直播","图片","跟帖","NBA","态度公开课","推荐","热点","社会","趣图","美女","军事"};grIDVIEw.setAdapter(new DragGrIDVIEw.Adapter() {      @OverrIDe      public voID exchange(int startposition,int endPositon) {        super.exchange(startposition,endPositon);        String item = List.get(startposition);        if (startposition < endPositon) {          List.add(endPositon + 1,item);          List.remove(startposition);        } else {          List.add(endPositon,item);          List.remove(startposition + 1);        }        for (int i = 0; i < List.size(); i++) {          Log.e(TAG,"exchange: =" + List.get(i));        }        notifyDataSetChanged();      }  ...省略部分代码      @OverrIDe      public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {        //todo,这里需要优化,没有复用vIEws。也不能按传统方式服用vIEw,否则会造成拖动的vIEw空白//        if (convertVIEw == null) {        convertVIEw = getLayoutInflater().inflate(R.layout.item,parent,false);//        }        ((TextVIEw) convertVIEw.findVIEwByID(R.ID.tv)).setText(getItem(position));        if (isDrag && position == holdposition) {          convertVIEw.setVisibility(VIEw.INVISIBLE);        } else convertVIEw.setVisibility(VIEw.VISIBLE);        return convertVIEw;      }    });

本文到这就结束了,有需要的同学拿到轮子就可以直接使用了,谢谢!

不知道有没有眼尖的同学发现Adapterd的getVIEw方法中有个 todo需要优化。原因是这样:如果打开注释中的代码,复用convertVIEw,会造成grIDVIEw释放后的新位置一片空白,不知道什么原因,因此折中的方法就是每次都是新生成一个convertVIEw。

总结

以上所述是小编给大家介绍的AndroID自定义grIDVIEw仿头条频道拖动管理功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

总结

以上是内存溢出为你收集整理的Android自定义gridView仿头条频道拖动管理功能全部内容,希望文章能够帮你解决Android自定义gridView仿头条频道拖动管理功能所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存