之前的工作中,需要实现一个功能就是GrIDVIEw中的item可以自由拖动, 思考了一下,其实实现起来不是很困难,主要工作就是交换节点,以及拖动时的移动效果,下面讲讲具体的实现:
首先声明一个BaseAdapter:
package com.dafasoft.dragablegrIDvIEw;import androID.content.Context;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.AbsListVIEw;import androID.Widget.AdapterVIEw;import androID.Widget.BaseAdapter;import java.util.ArrayList;import java.util.List;/*** Created by zhangyulong on 16/9/22.*/public abstract class BaseDragableAdapter<T> extends BaseAdapter {protected List<T> mList;protected Context mContext;protected LayoutInflater mInflater;//初始化需隐藏的位置信息protected int mHIDeposition = AdapterVIEw.INVALID_position;public BaseDragableAdapter(Context context) {this.mContext = context;this.mInflater = LayoutInflater.from(context);mList = new ArrayList<T>();}@OverrIDepublic int getCount() {return mList == null ? 0 : mList.size();}@OverrIDepublic Object getItem(int i) {return mList == null ? 0 : mList.get(i);}@OverrIDepublic long getItemID(int i) {return i;}@OverrIDepublic abstract VIEw getVIEw(int i,VIEw vIEw,VIEwGroup vIEwGroup);public voID setList(List<T> List) {if (this.mList != null) this.mList.clear(); // 避免脏数据if (List == null) {return;}this.mList = List;}public voID setList(T[] List){ArrayList<T> arrayList = new ArrayList<T>(List.length);for (T t : List) {arrayList.add(t);}setList(arrayList);}public List<T> getList() {return this.mList;}public voID addAll(List<T> List) {if (List == null || List.size() == 0) return;this.mList.addAll(List);}public voID addAll(int location,List<T> List) {if (List == null || List.size() == 0) return;this.mList.addAll(location,List);}public voID clean() {if (getCount() == 0) return;mList.clear();}public voID hIDeVIEw(int position){mHIDeposition = position;notifyDataSetChanged();}public voID showHIDeVIEw(){//重置hIDepositionmHIDeposition = AdapterVIEw.INVALID_position;notifyDataSetChanged();}/*** 交换节点* @param draggedPos 拖拽的起始节点* @param currentPos 拖拽的当前节点*/public voID swapVIEw(int draggedPos,int currentPos) {//从前往后拖if(draggedPos < currentPos){//将被拖拽的节点移动至当前节点mList.add(currentPos + 1,mList.get(draggedPos));//删除拖拽前的节点mList.remove(draggedPos);}//从后往前拖else if (draggedPos > currentPos) {//将被拖拽的节点移动至当前节点mList.add(currentPos,mList.get(draggedPos));//删除拖拽前的节点mList.remove(draggedPos + 1);}mHIDeposition = currentPos;notifyDataSetChanged();}}
其次实现DragableGrIDVIEw:
package com.dafasoft.dragablegrIDvIEw;import androID.content.Context;import androID.graphics.Bitmap;import androID.graphics.PixelFormat;import androID.util.AttributeSet;import androID.util.Log;import androID.vIEw.Gravity;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.WindowManager;import androID.Widget.AdapterVIEw;import androID.Widget.GrIDVIEw;import androID.Widget.ImageVIEw;/*** Created by zhangyulong on 16/9/22.*/public class DragableGrIDVIEw extends GrIDVIEw {private static final int DRAG_img_SHOW = 1;private static final int DRAG_img_NOT_SHOW = 0;private static final String LOG_TAG = "DragGrIDVIEw";private static final float AMP_FACTOR = 1.2f;private ImageVIEw dragImageVIEw;private WindowManager.LayoutParams dragImageVIEwParams;private WindowManager windowManager;private boolean isVIEwOnDrag = false;/**prevIoUs dragged over position*/private int preDraggedOverPositon = AdapterVIEw.INVALID_position;private int downRawX;private int downRawY;public DragableGrIDVIEw(Context context) {super(context);initVIEw();}public DragableGrIDVIEw(Context context,AttributeSet attrs) {super(context,attrs);initVIEw();}public DragableGrIDVIEw(Context context,AttributeSet attrs,int defStyleAttr) {super(context,attrs,defStyleAttr);initVIEw();}public voID initVIEw() {setonItemLongClickListener(mOnItemLongClickListener);//setonItemClickListener(onItemClickListener);//setonItemSelectedListener(onItemSelectedListener);//初始化显示被拖动item的image vIEwdragImageVIEw = new ImageVIEw(getContext());dragImageVIEw.setTag(DRAG_img_NOT_SHOW);//初始化用于设置dragImageVIEw的参数对象dragImageVIEwParams = new WindowManager.LayoutParams();//获取窗口管理对象,用于后面向窗口中添加dragImageVIEwwindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);}private OnItemLongClickListener mOnItemLongClickListener = new OnItemLongClickListener() {@OverrIDepublic boolean onItemLongClick(AdapterVIEw<?> parent,int position,long ID) {//记录长按item位置preDraggedOverPositon = position;//获取被长按item的drawing cachevIEw.destroyDrawingCache();vIEw.setDrawingCacheEnabled(true);//通过被长按item,获取拖动item的bitmapBitmap dragBitmap = Bitmap.createBitmap(vIEw.getDrawingCache());//设置拖动item的参数dragImageVIEwParams.gravity = Gravity.top | Gravity.left;//设置拖动item为原item 1.2倍dragImageVIEwParams.wIDth = (int)(AMP_FACTOR*dragBitmap.getWIDth());dragImageVIEwParams.height = (int)(AMP_FACTOR*dragBitmap.getHeight());//设置触摸点为绘制拖动item的中心dragImageVIEwParams.x = (downRawX - dragImageVIEwParams.wIDth/2);dragImageVIEwParams.y = (downRawY - dragImageVIEwParams.height/2);dragImageVIEwParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_NOT_touchABLE| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;//设置窗口支持透明度dragImageVIEwParams.format = PixelFormat.TRANSLUCENT;dragImageVIEwParams.windowAnimations = 0;//dragImageVIEw为被拖动item的容器,清空上一次的显示if((int)dragImageVIEw.getTag() == DRAG_img_SHOW) {windowManager.removeVIEw(dragImageVIEw);dragImageVIEw.setTag(DRAG_img_NOT_SHOW);}//设置本次被长按的itemdragImageVIEw.setimageBitmap(dragBitmap);//添加拖动item到屏幕windowManager.addVIEw(dragImageVIEw,dragImageVIEwParams);dragImageVIEw.setTag(DRAG_img_SHOW);isVIEwOnDrag = true;//设置被长按item不显示((BaseDragableAdapter)getAdapter()).hIDeVIEw(position);return true;}};@OverrIDepublic boolean ontouchEvent(MotionEvent ev) {//被按下时记录按下的坐标if(ev.getAction() == MotionEvent.ACTION_DOWN) {//获取触摸点相对于屏幕的坐标downRawX = (int)ev.getRawX();downRawY = (int)ev.getRawY();}//dragImageVIEw处于被拖动时,更新dragImageVIEw位置else if((ev.getAction() == MotionEvent.ACTION_MOVE) && (isVIEwOnDrag == true)) {Log.i(LOG_TAG,"" + ev.getRawX() + " " + ev.getRawY());//设置触摸点为dragImageVIEw中心dragImageVIEwParams.x = (int)(ev.getRawX() - dragImageVIEw.getWIDth()/2);dragImageVIEwParams.y = (int)(ev.getRawY() - dragImageVIEw.getHeight()/2);//更新窗口显示,移动dragImageVIEw的位置windowManager.updateVIEwLayout(dragImageVIEw,dragImageVIEwParams);//获取当前触摸点的item positionint currDraggedposition = pointToposition((int)ev.getX(),(int)ev.getY());//如果当前停留位置item不等于上次停留位置的item,交换本次和上次停留的itemif((currDraggedposition != AdapterVIEw.INVALID_position) && (currDraggedposition != preDraggedOverPositon)) {((BaseDragableAdapter)getAdapter()).swapVIEw(preDraggedOverPositon,currDraggedposition);preDraggedOverPositon = currDraggedposition;}}//释放dragImageVIEwelse if((ev.getAction() == MotionEvent.ACTION_UP) && (isVIEwOnDrag == true)) {((BaseDragableAdapter)getAdapter()).showHIDeVIEw();if((int)dragImageVIEw.getTag() == DRAG_img_SHOW) {windowManager.removeVIEw(dragImageVIEw);dragImageVIEw.setTag(DRAG_img_NOT_SHOW);}isVIEwOnDrag = false;}return super.ontouchEvent(ev);}}
实现一个adapter继承自BaseDragableAdapter:
package com.dafasoft.dragablegrIDvIEw;import androID.content.Context;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.TextVIEw;/*** Created by zhangyulong on 16/9/22.*/public class MyAdapter extends BaseDragableAdapter<String> {public MyAdapter(Context context) {super(context);}@OverrIDepublic VIEw getVIEw(int i,VIEwGroup vIEwGroup) {VIEwHolder vIEwHolder;if (vIEw == null) {vIEwHolder = new VIEwHolder();vIEw = mInflater.inflate(R.layout.adapter_my,null);vIEwHolder.mItemTv = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_item);vIEw.setTag(vIEwHolder);} else {vIEwHolder = (VIEwHolder)vIEw.getTag();}if (mHIDeposition == i) {vIEw.setVisibility(VIEw.GONE);} else {vIEw.setVisibility(VIEw.VISIBLE);}vIEwHolder.mItemTv.setText(mList.get(i));return vIEw;}class VIEwHolder{TextVIEw mItemTv;}}
在activity中使用:
package com.dafasoft.dragablegrIDvIEw;import androID.app.Activity;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import java.util.ArrayList;import java.util.List;public class MainActivity extends Activity {private DragableGrIDVIEw mDragableGv;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_main);mDragableGv = (DragableGrIDVIEw) findVIEwByID(R.ID.drag_grID_vIEw);MyAdapter adapter = new MyAdapter(this);List<String> List = new ArrayList<String>();for (int i = 0 ; i < 30 ; i ++) {List.add("position" + i);}adapter.setList(List);mDragableGv.setAdapter(adapter);}}
以上所述是小编给大家介绍的AndroID实现GrIDVIEw中的item自由拖动效果,实现一个模拟后台数据登入的效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android实现GridView中的item自由拖动效果全部内容,希望文章能够帮你解决Android实现GridView中的item自由拖动效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)