Android HorizontalScrollView内子控件横向拖拽实例代码
程序员文章站
2024-03-04 21:57:54
前言
网上listview上下拖动的例子有,效果也很好,但是项目要横着拖的,只要硬着...
前言
网上listview上下拖动的例子有,效果也很好,但是项目要横着拖的,只要硬着头皮自己写(主要是没找到合适的),参考文章1修改而来,分享一下。
正文
截图
实现代码:
public class hodragactivity extends activity { private linearlayout main; private gesturedetector mgesturedetector; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_cc); main = (linearlayout) findviewbyid(r.id.main); binddraplistener(r.id.myimage1); binddraplistener(r.id.myimage2); binddraplistener(r.id.myimage3); binddraplistener(r.id.myimage4); binddraplistener(r.id.myimage5); binddraplistener(r.id.myimage6); binddraplistener(r.id.myimage7); binddraplistener(r.id.myimage8); binddraplistener(r.id.myimage9); binddraplistener(r.id.myimage10); binddraplistener(r.id.myimage11); binddraplistener(r.id.myimage12); mgesturedetector = new gesturedetector(this, new drapgesturelistener()); } private view mdrapview; private void binddraplistener(int id) { view v = findviewbyid(id); v.setontouchlistener(montouchlistener); v.setondraglistener(mondraglistener); } private ontouchlistener montouchlistener = new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { mdrapview = v; if (mgesturedetector.ontouchevent(event)) return true; switch (event.getaction() & motionevent.action_mask) { case motionevent.action_up: break; } return false; } }; private ondraglistener mondraglistener = new ondraglistener() { @override public boolean ondrag(view v, dragevent event) { switch (event.getaction()) { case dragevent.action_drag_started: // do nothing break; case dragevent.action_drag_entered: v.setalpha(0.5f); break; case dragevent.action_drag_exited: v.setalpha(1f); break; case dragevent.action_drop: view view = (view) event.getlocalstate(); for (int i = 0, j = main.getchildcount(); i < j; i++) { if (main.getchildat(i) == v) { // 当前位置 main.removeview(view); main.addview(view, i); break; } } break; case dragevent.action_drag_ended: v.setalpha(1f); default: break; } return true; } }; private class drapgesturelistener extends simpleongesturelistener { @override public boolean onsingletapconfirmed(motionevent e) { return super.onsingletapconfirmed(e); } @override public void onlongpress(motionevent e) { super.onlongpress(e); clipdata data = clipdata.newplaintext("", ""); mydragshadowbuilder shadowbuilder = new mydragshadowbuilder( mdrapview); mdrapview.startdrag(data, shadowbuilder, mdrapview, 0); } @override public boolean ondown(motionevent e) { return true; } } private class mydragshadowbuilder extends view.dragshadowbuilder { private final weakreference<view> mview; public mydragshadowbuilder(view view) { super(view); mview = new weakreference<view>(view); } @override public void ondrawshadow(canvas canvas) { canvas.scale(1.5f, 1.5f); super.ondrawshadow(canvas); } @override public void onprovideshadowmetrics(point shadowsize, point shadowtouchpoint) { // super.onprovideshadowmetrics(shadowsize, shadowtouchpoint); final view view = mview.get(); if (view != null) { shadowsize.set((int) (view.getwidth() * 1.5f), (int) (view.getheight() * 1.5f)); shadowtouchpoint.set(shadowsize.x / 2, shadowsize.y / 2); } else { // log.e(view.view_log_tag, // "asked for drag thumb metrics but no view"); } } } }
代码说明
1、 mydragshadowbuilder主要用于缩放拖拽的imageview,这里放大了1.5倍,而又不至于影响原控件,这里是放大两倍
2、这里是通过长按来唤起拖拽操作,所以监听了onlongpress。
3、注意拖拽ondraglistener是android 3.0才新增的api。
以上就是android horizontalscrollview内子控件横向拖拽实例,欢迎交流,有需要参考下。
上一篇: Java:IO