Android小Demo——OnDragListener
2016-01-19 10:56
721 查看
今天看到一篇挺有趣的文章,讲了Drag和Drop动作,例子简单,跟着文章动手敲了一遍。原文链接 和原文的GitHub仓库
先上布局代码,很简单,就是一个九宫格。
下面就是DragAndDrop的代码,也是十分简单。
上面这段代码,有几个稍微值得注意的知识点。
使用了ClipData复制TextView的内容,ClipData相关用法大家可以度娘。
DragShadowBuilder的作用是创建一个Drag或Drop操作时的图片,这里使用了默认值,创建了一个当前视图的图片。大家可以使用它来创建其它图片。
DragEvent的几个动作,ACTION_DRAG_STARTED–表示view开始drag,返回true表示这个view的drag和drop动作可用。
ACTION_DRAG_ENTERED–表示有drag的view进入到当前view的区域
ACTION_DRAG_LOCATION–表示drag的view在当前的view的区域中,改变了它的位置
ACTION_DRAG_EXITED–表示drag的view离开了当前的view的区域
ACTION_DROP–真正的drop操作,就是你的drag动作松开手指
ACTION_DRAG_ENDED–drag和drop操作都结束。
是不是很简单?最后上个效果图。
先上布局代码,很简单,就是一个九宫格。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layout_outside" tools:context=".MainActivity"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:textSize="40sp" android:layout_gravity="left|top" /> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:textSize="40sp" android:layout_gravity="center_horizontal|top" /> <TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:textSize="40sp" android:layout_gravity="right|top" /> <TextView android:id="@+id/text4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" android:textSize="40sp" android:layout_gravity="left|center_vertical" /> <TextView android:id="@+id/text5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" android:textSize="40sp" android:layout_gravity="center" /> <TextView android:id="@+id/text6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6" android:textSize="40sp" android:layout_gravity="right|center_vertical" /> <TextView android:id="@+id/text7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="7" android:textSize="40sp" android:layout_gravity="left|bottom" /> <TextView android:id="@+id/text8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="8" android:textSize="40sp" android:layout_gravity="center_horizontal|bottom" /> <TextView android:id="@+id/text9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9" android:textSize="40sp" android:layout_gravity="right|bottom" /> </FrameLayout>
下面就是DragAndDrop的代码,也是十分简单。
public void initUI() { FrameLayout layout = (FrameLayout)findViewById(R.id.layout_outside); for (int i = 0;i < layout.getChildCount();i++) { final TextView view = (TextView) layout.getChildAt(i); view.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { ClipData data = ClipData.newPlainText("value",view.getText()); view.startDrag(data,new View.DragShadowBuilder(v),null,0); return true; } }); view.setOnDragListener(new View.OnDragListener() { @Override public boolean onDrag(View v, DragEvent event) { switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: return true; case DragEvent.ACTION_DRAG_ENTERED: v.setBackgroundColor(Color.CYAN); return true; case DragEvent.ACTION_DRAG_LOCATION: return true; case DragEvent.ACTION_DRAG_EXITED: v.setBackgroundColor(Color.RED); return true; case DragEvent.ACTION_DROP: v.setBackgroundColor(Color.GREEN); //得到拖动的值 int dragVal = Integer.parseInt(event.getClipData().getItemAt(0).getText().toString()); //得到textview的值 int viewVal = Integer.parseInt(((TextView) v).getText().toString()); ((TextView) v).setText("" + (dragVal + viewVal)); return true; case DragEvent.ACTION_DRAG_ENDED: return true; } return false; } }); } }
上面这段代码,有几个稍微值得注意的知识点。
使用了ClipData复制TextView的内容,ClipData相关用法大家可以度娘。
DragShadowBuilder的作用是创建一个Drag或Drop操作时的图片,这里使用了默认值,创建了一个当前视图的图片。大家可以使用它来创建其它图片。
DragEvent的几个动作,ACTION_DRAG_STARTED–表示view开始drag,返回true表示这个view的drag和drop动作可用。
ACTION_DRAG_ENTERED–表示有drag的view进入到当前view的区域
ACTION_DRAG_LOCATION–表示drag的view在当前的view的区域中,改变了它的位置
ACTION_DRAG_EXITED–表示drag的view离开了当前的view的区域
ACTION_DROP–真正的drop操作,就是你的drag动作松开手指
ACTION_DRAG_ENDED–drag和drop操作都结束。
是不是很简单?最后上个效果图。
相关文章推荐
- android权限的分类
- AndroidStudio工程加入svn步骤
- 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗? Android studio乱码
- android 列表倒计时流畅的完美实现
- 推荐几款实用的Android Studio 插件
- Android高效加载大图、多图解决方案,有效避免程序OOM
- Android测试环境配置
- Android性能优化典范
- android自带图片资源图标一览,android.R.drawable
- Android应用内打开QQ聊天框
- android shape的使用
- Android--AndroidManifest 中original-package标签
- android之android studio的NDK环境搭建
- Android Studio常用插件
- Android百度地图实现搜索和定位及自定义图标绘制并点击时弹出泡泡
- Android开发笔记(五十二)通知推送Notification
- (Android图片内存优化)Picasso加载图片 教程。。详细版
- Android高德地图开发(3)——路况+定位
- Android Studio 打包发布流程
- Android开发环境搭建ADT-Bundle及Hello World