聊聊GridView实现拖拽排序及数据交互的问题
程序员文章站
2022-03-12 22:34:56
在研究项目中的一个效果的时候,查找资料过程中发现有人有这么一种需求,就是gridview在实现拖拽排序的基础上,如果是两个gridview之间实现拖拽效果,并要实现数据交互。一、效果图:实现这个效果需...
在研究项目中的一个效果的时候,查找资料过程中发现有人有这么一种需求,就是gridview在实现拖拽排序的基础上,如果是两个gridview之间实现拖拽效果,并要实现数据交互。
一、效果图:
实现这个效果需要考虑的事情:
① 这个ui整体的构建以及事件的传递
② 如何实现view的共享,穿过边界
二、实现思路:
对于单个gridview的拖拽效果实现是参考网上的一篇文章,很抱歉没能及时保存地址,所以这里就不贴了。
整体实现: 一个容器(dragchessview)里放置两个dragview(可拖拽排序item的view),事件全部交给dragchessview去处理,然后由他去控制事件的分配和处理。在这个容器中还在这两个dragview上面覆盖一层framelayout,来放置要移动的那个view,然后根据手势去移动view。dragview里面进行view的动态交换以及数据交换。
三、实现代码:
1. dragchessview代码
public class dragchessview extends framelayout { private gesturedetector detector; /** * 点击拖动 */ public static final int drag_when_touch = 0; /** * 长按拖动 */ public static final int drag_by_long_click = 1; private int mdragmode = drag_when_touch; private boolean hassenddragmsg = false; private handler handler = new handler(new handler.callback() { @override public boolean handlemessage(message msg) { switch (msg.what) { case 0x123: // 启动拖拽模式 isdragable = true; // 根据点击的位置生成该位置上的view镜像 if (istouchintop(msg.arg2)) { mdragtop.setcurrentdragposition(msg.arg1); copyview(mdragtop); } else { mdragbottom.setcurrentdragposition(msg.arg1); copyview(mdragbottom); } hassenddragmsg = false; break; default: break; } return false; } }); private boolean isdragable = true; private float[] lastlocation = null; private view mcopyview; private ontouchlistener l; // 转交给gridview一些常用监听器 private adapterview.onitemlongclicklistener itemlongclicklistener; private int mtoucharea = 0; private view dragslider; private point mmovepoint; // 记录移动走向,上到下,还是下到上 /** * @param itemclicklistener * @描述:item 转交给gridview一些常用监听器 */ public void setonitemclicklistener(adapterview.onitemclicklistener itemclicklistener) { mdragbottom.setonitemclicklistener(itemclicklistener); } /** * 长按监听器自己触发,点击拖动模式不存在长按 * * @param */ public void setonitemlongclicklistener(adapterview.onitemlongclicklistener itemlongclicklistener) { this.itemlongclicklistener = itemlongclicklistener; } private boolean canaddviewwhendragchange = true; private int mstartpoint; private int start_drag_top = 0; private int start_drag_bottom = 1; /** * 手势监听器,滚动和单击 */ private gesturedetector.simpleongesturelistener simpleongesturelistener = new gesturedetector.simpleongesturelistener() { @override public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { if (hassenddragmsg) { hassenddragmsg = false; handler.removemessages(0x123); } if (isdragable && mcopyview != null) { if (lastlocation == null && e1 != null) { lastlocation = new float[]{e1.getrawx(), e1.getrawy()}; } if (lastlocation == null) lastlocation = new float[]{0, 0}; distancex = lastlocation[0] - e2.getrawx(); distancey = lastlocation[1] - e2.getrawy(); lastlocation[0] = e2.getrawx(); lastlocation[1] = e2.getrawy(); mcopyview.setx(mcopyview.getx() - distancex); mcopyview.sety(mcopyview.gety() - distancey); int to = eventtoposition(e2); mcopyview.invalidate(); if (isdragintop()) { if (isdragfrombottom()) { if (isdragback(isdragintop())) { //针对已经进入bottom区域,但是又返回来的情况 mstartpoint = start_drag_bottom; //切换,保证移动过程中只执行一次 canaddviewwhendragchange = true; } if (canaddviewwhendragchange) {// 保证移动过程中,数据只有一次的添加 mdragtop.addswapview(mdragbottom.getswapdata()); mdragbottom.removeswapview(); canaddviewwhendragchange = false; if (hideview != null) hideview.setvisibility(visible); } if (mdragtop.isviewinitdone()) { mdragtop.setcurrentdragposition(mdragtop.getgridchildcount() - 1); hideview = mdragtop.getgridchildat(mdragtop.getcurrentdragposition()); if (hideview != null) hideview.setvisibility(invisible); mmovepoint = getdragviewcenterpoint(mdragtop); } } if (mdragtop.isviewinitdone()) dragchangeposition(mdragtop, to); } else { if (isdragfromtop()) { if (isdragback(isdragintop())) { mstartpoint = start_drag_top; canaddviewwhendragchange = true; } if (canaddviewwhendragchange) { mdragbottom.addswapview(mdragtop.getswapdata()); mdragtop.removeswapview(); canaddviewwhendragchange = false; if (hideview != null) hideview.setvisibility(visible); } if (mdragbottom.isviewinitdone()) { mdragbottom.setcurrentdragposition(mdragbottom.getgridchildcount() - 1); hideview = mdragbottom.getgridchildat(mdragbottom.getcurrentdragposition()); if (hideview != null) hideview.setvisibility(invisible); log.e("mmovepoint", mmovepoint.x + "-----------" + mmovepoint.y); mmovepoint = getdragviewcenterpoint(mdragbottom); } } if (mdragbottom.isviewinitdone()) dragchangeposition(mdragbottom, to); } } return true; } @override public void onshowpress(motionevent e) { /** 响应长按拖拽 */ if (mdragmode == drag_by_long_click) { // 启动拖拽模式 // isdragable = true; // 通知父控件不拦截我的事件 getparent().requestdisallowintercepttouchevent(true); // 根据点击的位置生成该位置上的view镜像 int position = eventtoposition(e); if (iscandragmove(istouchintop(e) ? mdragtop : mdragbottom, position)) { // copyview(currentdragposition = position); message msg = handler.obtainmessage(0x123, position, (int) e.gety()); // showpress本身大概需要170毫秒 handler.sendmessagedelayed(msg, draglongpresstime - 170); mmovepoint = new point((int) e.getx(), (int) e.gety()); mstartpoint = istouchintop(e) ? start_drag_top : start_drag_bottom; hassenddragmsg = true; } } } }; private boolean isdragback(boolean dragintop) { return (dragintop && mstartpoint == start_drag_top) || (!dragintop && mstartpoint == start_drag_bottom); } private boolean isdragfromtop() { if (mmovepoint != null && mdragtop != null) { if ((mmovepoint.x > mdragtop.getx() && mmovepoint.x < (mdragtop.getx() + mdragtop.getwidth())) && (mmovepoint.y > mdragtop.gety() && mmovepoint.y < (mdragtop.gety() + mdragtop.getheight()))) { return true; } } return false; } private point getdragviewcenterpoint(dragview dragview) { point result = new point(); if (dragview != null) { int height = dragview.getheight(); int width = dragview.getwidth(); float x = dragview.getx(); float y = dragview.gety(); result.set((int) (x + width / 2), (int) (y + height / 2)); } return result; } private boolean isdragfrombottom() { if (mmovepoint != null && mdragbottom != null) { if ((mmovepoint.x > mdragbottom.getx() && mmovepoint.x < (mdragbottom.getx() + mdragbottom.getwidth())) && (mmovepoint.y > mdragbottom.gety() && mmovepoint.y < (mdragbottom.gety() + mdragbottom.getheight()))) { return true; } } return false; } private boolean istouchintop(motionevent event) { float y = event.gety(); return istouchintop(y); } private boolean istouchintop(float y) { return y > mdragtop.gety() && y < (mdragtop.gety() + mdragtop.getheight()); } private void dragchangeposition(dragview dragview, int to) { if (to != dragview.getcurrentdragposition() && iscandragmove(dragview, to)) { dragview.ondragpositionchange(dragview.getcurrentdragposition(), to); } } private boolean iscandragmove(dragview dragview, int position) { return position >= dragview.getheaddragposition() && position < dragview.getgridchildcount() - dragview.getfootdragposition(); } private framelayout mdragframe; private dragview mdragbottom; private dragview mdragtop; private view hideview; private long draglongpresstime = 600; public dragchessview(@nonnull context context) { this(context, null); } public dragchessview(@nonnull context context, @nullable attributeset attrs) { this(context, attrs, -1); } public dragchessview(@nonnull context context, @nullable attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(attrs); } private void init(attributeset attrs) { context context = getcontext(); detector = new gesturedetector(context, simpleongesturelistener); detector.setislongpressenabled(false); mdragframe = new framelayout(context); dragslider = layoutinflater.from(context).inflate(r.layout.layout_drag_chess, this, false); mdragtop = dragslider.findviewbyid(r.id.drag_top); mdragbottom = dragslider.findviewbyid(r.id.drag_bottom); addview(dragslider, -1, -1); addview(mdragframe, -1, -1); } @override public boolean ontouchevent(motionevent ev) { if (l != null) { l.ontouch(this, ev); } if (!isviewinitdone()) { return false; } if (isdragable) { handlescrollandcremirror(ev); } else { // 交给子控件自己处理 dispatchevent(istouchintop(ev) ? mdragtop : mdragbottom, ev); } // 处理拖动 detector.ontouchevent(ev); if (ev.getaction() == motionevent.action_cancel || ev.getaction() == motionevent.action_up) { lastlocation = null; if (hassenddragmsg) { hassenddragmsg = false; handler.removemessages(0x123); } } return true; } private void dispatchevent(dragview dragview, motionevent ev) { dragview.dispatchevent(ev); } private boolean isdragintop() { if (mcopyview == null) return false; return (mcopyview.gety() + mcopyview.getheight()) < (mdragtop.gety() + mdragtop.getbottom()); } /** * description :拦截所有事件 */ @override public boolean onintercepttouchevent(motionevent ev) { return true; } /** * 处理自动滚屏,和单击生成镜像 */ private void handlescrollandcremirror(motionevent ev) { switch (ev.getaction()) { case motionevent.action_down: // 通知父控件不拦截我的事件 getparent().requestdisallowintercepttouchevent(true); // 根据点击的位置生成该位置上的view镜像 int position = eventtoposition(ev); makecopyview(istouchintop(ev) ? mdragtop : mdragbottom, position); break; case motionevent.action_move: getparent().requestdisallowintercepttouchevent(true);// 通知父控件不拦截我的事件 // 内容太多时,移动到边缘会自动滚动 decodescrollarea(isdragintop() ? mdragtop : mdragbottom, ev); break; case motionevent.action_cancel: case motionevent.action_up: if (hideview != null) { hideview.setvisibility(view.visible); if (ondragselectlistener != null) { ondragselectlistener.onputdown(hideview); } } mdragframe.removeallviews(); // mdragframe.scrollto(0, 0); // isnotifybydragsort = true; updateui(isdragintop() ? mdragtop : mdragbottom, ev); mcopyview = null; canaddviewwhendragchange = true; // 放手时取消拖动排序模式 if (mdragmode == drag_by_long_click) { isdragable = false; } break; default: break; } } private void updateui(dragview dragview, motionevent ev) { if (dragview.ishaspositionchange()) { dragview.sethaspositionchange(false); dragview.getadapter().notifydatasetchanged(); } else if (mdragmode == drag_by_long_click && itemlongclicklistener != null) { dragview.onitemlongclick(itemlongclicklistener); } // 停止滚动 if (dragview.iscanscroll()) { int scrollstates2 = dragview.decodetoucharea(ev); if (scrollstates2 != 0) { dragview.ontouchareachange(0); mtoucharea = 0; } } } private void decodescrollarea(dragview dragview, motionevent ev) { if (dragview.iscanscroll()) { int toucharea = dragview.decodetoucharea(ev); if (toucharea != mtoucharea) { dragview.ontouchareachange(toucharea); mtoucharea = toucharea; } } } private void makecopyview(dragview dragview, int position) { if (position >= dragview.getheaddragposition() && position < dragview.getgridchildcount() - dragview.getfootdragposition()) { dragview.setcurrentdragposition(position); copyview(dragview); } } /** * 得到事件触发点, 摸到的是哪一个item */ public int eventtoposition(motionevent ev) { if (ev != null) { if (istouchintop(ev)) return mdragtop.eventtoposition(ev); return mdragbottom.eventtoposition(ev); } return 0; } /** * 复制一个镜像,并添加到透明层 */ private void copyview(dragview dragview) { // todo: 2018/4/2 创建可移动的 item hideview = dragview.getgridchildat(dragview.getcurrentdragposition()); int realposition = dragview.getgridchildpos(hideview); dragadapter adapter = dragview.getadapter(); if (!adapter.isusecopyview()) { mcopyview = adapter.getview(realposition, mcopyview, mdragframe); } else { mcopyview = adapter.copyview(realposition, mcopyview, mdragframe); } hideview.setvisibility(view.invisible); if (mcopyview.getparent() == null) mdragframe.addview(mcopyview, dragview.getmcolwidth(), dragview.getmcolheight()); int[] l1 = new int[2]; int[] l2 = new int[2]; hideview.getlocationonscreen(l1); mdragframe.getlocationonscreen(l2); // mcopyview.setx(hideview.getleft()); // mcopyview.sety(hideview.gettop() - mcurrenty); mcopyview.setx(l1[0] - l2[0]); mcopyview.sety(l1[1] - l2[1]); if (ondragselectlistener == null) { mcopyview.setscalex(1.2f); mcopyview.setscaley(1.2f); } else { ondragselectlistener.ondragselect(mcopyview); } } private dragsortgridview.ondragselectlistener ondragselectlistener; /** * @描述:一个item view刚被拖拽和放下时起来生成镜像时调用. */ public void setondragselectlistener(dragsortgridview.ondragselectlistener ondragselectlistener) { this.ondragselectlistener = ondragselectlistener; } /** * @param mode int类型 * @描述:设置拖动的策略是点击还是长按 */ public void setdragmodel(int mode) { this.mdragmode = mode; isdragable = mode == drag_when_touch; } public void setanimframe(listenframelayout mdragframe) { this.mdragframe = mdragframe; } /** * 设置长按需要用时 * * @param time */ public void setdraglongpresstime(long time) { draglongpresstime = time; } public void setbottomadapter(@notnull dragadapter adapter) { mdragbottom.setadapter(adapter); } public boolean isviewinitdone() { boolean result = mdragbottom.isviewinitdone(); if (mdragtop.getvisibility() == visible) result &= mdragtop.isviewinitdone(); return result; } public void settopadapter(@notnull dragadapter adapter) { mdragtop.setadapter(adapter); } }
2. dragview的实现:
public class dragview<t> extends framelayout { private static final int tag_key = r.id.first; private noscrollgridview mgridview; private list<view> mchilds = new arraylist<view>(); protected int mnumcolumns = 3; protected int mcolheight = 0; protected int mcolwidth = 0; protected int mchildcount = 0; protected int mmaxheight = 0; private boolean isviewinitdone = false; private listenscrollview mscrollview; private int mcurrenty = 0; /** * 自动滚屏的动画 */ private valueanimator animator; private dragadapter adapter; private int headdragposition = 0; private int footdragposition = 0; private int currentdragposition = -1; /** * 是否有位置发生改变,否则不用重绘 */ private boolean haspositionchange = false; /** * gridview能否滚动,是否内容太多 */ private boolean canscroll = true; /** * 动画时间 */ private static final long anim_during = 250; private object swapedata; public dragview(@nonnull context context) { this(context, null); } public dragview(@nonnull context context, @nullable attributeset attrs) { this(context, attrs, -1); } public dragview(@nonnull context context, @nullable attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(); } private void init() { context context = getcontext(); mgridview = new noscrollgridview(context); mgridview.setverticalscrollbarenabled(false); mgridview.setstretchmode(gridview.stretch_column_width); mgridview.setselector(new colordrawable()); // view的宽高之类必须在测量,布局,绘制一系列过程之后才能获取到 mgridview.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { if (mchilds.isempty()) { for (int i = 0; i < mgridview.getchildcount(); i++) { view view = mgridview.getchildat(i); view.settag(tag_key, new int[]{0, 0}); view.clearanimation(); mchilds.add(view); } } if (!mchilds.isempty()) { mcolheight = mchilds.get(0).getheight(); } mcolwidth = mgridview.getcolumnwidth(); if (mchildcount % mnumcolumns == 0) { mmaxheight = mcolheight * mchildcount / mnumcolumns; } else { mmaxheight = mcolheight * (mchildcount / mnumcolumns + 1); } canscroll = mmaxheight - getheight() > 0; // 告知事件处理,完成view加载,许多属性也已经初始化了 isviewinitdone = true; } }); mscrollview = new listenscrollview(context); mgridview.setnumcolumns(mnumcolumns); mscrollview.addview(mgridview, -1, -1); addview(mscrollview, -1, -1); } private datasetobserver observer = new datasetobserver() { @override public void onchanged() { mchildcount = adapter.getcount(); // 下列属性状态清除,才会在被调用notifydatasetchange时,在gridview测量布局完成后重新获取 mchilds.clear(); mcolheight = mcolwidth = mmaxheight = 0; isviewinitdone = false; } @override public void oninvalidated() { mchildcount = adapter.getcount(); } }; /** * 控制自动滚屏的动画监听器. */ private valueanimator.animatorupdatelistener animupdatelistener = new valueanimator.animatorupdatelistener() { @override public void onanimationupdate(valueanimator animation) { int targety = math.round((float) animation.getanimatedvalue()); if (targety < 0) { targety = 0; } else if (targety > mmaxheight - getheight()) { targety = mmaxheight - getheight(); } // mgridview.scrollto(0, targety); mscrollview.smoothscrollto(0, targety); // mcurrenty = targety; } }; public void setadapter(dragadapter adapter) { if (this.adapter != null && observer != null) { this.adapter.unregisterdatasetobserver(observer); } this.adapter = adapter; mgridview.setadapter(adapter); adapter.registerdatasetobserver(observer); mchildcount = adapter.getcount(); } /** * @param from * @param to * @描述:动画效果移动view */ public void translateview(int from, int to) { view view = mchilds.get(from); int fromxvalue = ((int[]) view.gettag(tag_key))[0]; int fromyvalue = ((int[]) view.gettag(tag_key))[1]; int toxvalue = to % mnumcolumns - from % mnumcolumns + fromxvalue; int toyvalue = to / mnumcolumns - from / mnumcolumns + fromyvalue; animation animation = new translateanimation(1, fromxvalue, 1, toxvalue, 1, fromyvalue, 1, toyvalue); animation.setduration(anim_during); animation.setfillafter(true); view.settag(tag_key, new int[]{toxvalue, toyvalue}); view.startanimation(animation); } /** * @param from * @param to * @描述:拖动view使位置发生改变时 */ public void ondragpositionchange(int from, int to) { if (from > to) { for (int i = to; i < from; i++) { translateview(i, i + 1); } } else { for (int i = to; i > from; i--) { translateview(i, i - 1); } } if (!haspositionchange) { haspositionchange = true; } if ((from >= mchilds.size() || from < 0) || (to >= mchilds.size() || to < 0)) return; adapter.ondatamodelmove(from, to); view view = mchilds.remove(from); mchilds.add(to, view); currentdragposition = to; } /** * @param scrollstates * @描述:触摸区域改变,做相应处理,开始滚动或停止滚动 */ public void ontouchareachange(int scrollstates) { if (!canscroll) { return; } if (animator != null) { animator.removeupdatelistener(animupdatelistener); } if (scrollstates == 1) {// 从普通区域进入触发向上滚动的区域 int instance = mmaxheight - getheight() - mcurrenty; animator = valueanimator.offloat(mcurrenty, mmaxheight - getheight()); animator.setduration((long) (instance / 0.5f)); animator.settarget(mgridview); animator.addupdatelistener(animupdatelistener); animator.start(); } else if (scrollstates == -1) {// 进入触发向下滚动的区域 animator = valueanimator.offloat(mcurrenty, 0); animator.setduration((long) (mcurrenty / 0.5f)); animator.settarget(mgridview); animator.addupdatelistener(animupdatelistener); animator.start(); } } /** * @param ev 事件 * @return 0中间区域, 1底部,-1顶部 * @描述: 检查当前触摸事件位于哪个区域, 顶部1/5可能触发下滚,底部1/5可能触发上滚 */ public int decodetoucharea(motionevent ev) { if (ev.gety() > (getheight() + gety()) * 4 / (double) 5) { return 1; } else if (ev.gety() < (getheight() + gety()) / (double) 5) { return -1; } else { return 0; } } public boolean isviewinitdone() { return isviewinitdone; } /** * 设置前几个item不可以改变位置 */ public void setnopositionchangeitemcount(int count) { headdragposition = count; } /** * 设置后几个item不可以改变位置 */ public void setfootnopositionchangeitemcount(int count) { footdragposition = count; } public int getheaddragposition() { return headdragposition; } public int getfootdragposition() { return footdragposition; } public int getgridchildcount() { return mchilds.size(); } public view getgridchildat(int position) { if (position < 0 || position >= mchilds.size()) return null; return mchilds.get(position); } public int getgridchildpos(view view) { return mgridview.indexofchild(view); } public void setcurrentdragposition(int currentdragposition) { this.currentdragposition = currentdragposition; } public int getcurrentdragposition() { return currentdragposition; } public int getmcolheight() { return mcolheight; } public int getmcolwidth() { return mcolwidth; } public dragadapter getadapter() { return adapter; } public boolean ishaspositionchange() { return haspositionchange; } public void sethaspositionchange(boolean haspositionchange) { this.haspositionchange = haspositionchange; } public boolean iscanscroll() { return canscroll; } public void setonitemclicklistener(adapterview.onitemclicklistener onitemclicklistener) { mgridview.setonitemclicklistener(onitemclicklistener); } public void onitemlongclick(adapterview.onitemlongclicklistener itemlongclicklistener) { itemlongclicklistener.onitemlongclick(mgridview, childat(currentdragposition), currentdragposition, 0); } public view childat(int index) { return mgridview.getchildat(index); } public void dispatchevent(motionevent ev) { if (canscroll) mscrollview.dispatchtouchevent(ev); else mgridview.dispatchtouchevent(ev); } public int eventtoposition(motionevent ev) { if (ev != null) { int m = (int) ev.getx() / mcolwidth; int n = (int) (ev.gety() - gety() + mcurrenty) / mcolheight; int position = n * mnumcolumns + m; if (position >= mchildcount) { return mchildcount - 1; } else { return position; } } return 0; } public void addswapview(object data) { adapter.addnewdata(data); } public object getswapdata() { return adapter.getswapdata(currentdragposition); } public void removeswapview() { if (adapter != null) { adapter.removedata(currentdragposition); adapter.notifydatasetchanged(); } } class listenscrollview extends scrollview { public listenscrollview(context context) { super(context); } @override protected void onscrollchanged(int l, int t, int oldl, int oldt) { super.onscrollchanged(l, t, oldl, oldt); mcurrenty = getscrolly(); } } class noscrollgridview extends gridview { public noscrollgridview(context context) { super(context); } /** * @return * @描述:兼容老版本的getcolumwidth * @作者 [pwx273343] 2015年7月1日 */ public int getcolumnwidth() { return getwidth() / getnumcolumns(); } public noscrollgridview(context context, attributeset attrs) { super(context, attrs); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { int mexpandspec = measurespec.makemeasurespec(integer.max_value >> 2, measurespec.at_most); super.onmeasure(widthmeasurespec, mexpandspec); } } }
3. dragadapter的代码:
public abstract class dragadapter extends baseadapter { /** * @param from * @param to * @描述:当从from排序被拖到to排序时的处理方式,请对相应的数据做处理。 */ public abstract void ondatamodelmove(int from, int to); /** * 复制view使用的方法,默认直接使用getview方法获取 * * @param position * @param convertview * @param parent * @return */ public view copyview(int position, view convertview, viewgroup parent) { return null; } /** * 是否启用copyview方法 * * @return true 使用copyview复制 false 使用getview直接获取镜像 */ public boolean isusecopyview() { return false; } public abstract object getswapdata(int position); public abstract void removedata(int position); public void addnewdata(object data) { } }
4.使用mainactivity:
class griddragshortactivity : appcompatactivity() { override fun oncreate(savedinstancestate: bundle?) { super.oncreate(savedinstancestate) window.attributes.systemuivisibility = view.system_ui_flag_fullscreen or view.system_ui_flag_layout_fullscreen or view.system_ui_flag_immersive or view.system_ui_flag_hide_navigation setcontentview(r.layout.activity_grid_drag_short) //长按item响应该item的拖动排序,默认是触摸就开始拖动 drag_main.setdragmodel(dragsortgridview.drag_by_long_click) //设置每行个数 // drag_main.numcolumns = 3 var arraylist = arraylist<string>() var list = arraylist<string>() for (i in 0..29) { arraylist.add("" + i) } for (i in 0..29) { list.add(('a' + i).tostring()) } drag_main.setbottomadapter(customadapter(list, this)) drag_main.settopadapter(customadapter(arraylist, this)) } }
布局:activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.mdzz.dragchessview android:id="@+id/drag_main" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout> </relativelayout>
布局 layout_drag_chess.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.chineseall.bookshelf.mdzz.dragview android:id="@+id/drag_top" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#ffffff" /> <textview android:id="@+id/folder_title" android:layout_width="match_parent" android:layout_height="40dp" android:background="#0586f9" android:gravity="center" android:text="@string/text" android:textcolor="#ffffff" android:textsize="18sp" /> <com.chineseall.bookshelf.mdzz.dragview android:id="@+id/drag_bottom" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#ffffff" /> </linearlayout>
在实际使用中,可以更据需要再去拓展使用,代码演变过程中的另一个效果,来看看,以它结束这篇:
到此这篇关于gridview实现拖拽排序以及数据交互的文章就介绍到这了,更多相关gridview拖拽排序内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!