Today we will learn how to create android drag drop functionality using DragLinearLayout.

今天,我们将学习如何使用DragLinearLayout创建android拖放功能。

使用DragLinearLayout的Android拖放 (Android Drag Drop using DragLinearLayout)

Android DragLinearLayout library can be used in place of any LinearLayout. To do this we’ll add a third party library in our build.gradle file as follows:

可以使用Android DragLinearLayout库代替任何LinearLayout。 为此,我们将在build.gradle文件中添加第三方库,如下所示:

compile 'com.jmedeisis:draglinearlayout:1.1.0'

The above class library extends the LinearLayout and customises is to add the drag and drop feature.

上面的类库扩展了LinearLayout ,自定义项是添加拖放功能。

onTouchEvent method is overridden to programmatically detect the gesture movements and drag and drop accordingly. We’ll get into details of the android drag and drop feature with more details in a later tutorial.

重写onTouchEvent方法以编程方式检测手势运动并相应地拖放。 我们将在以后的教程中详细介绍android拖放功能。

Note: You can view the complete source code of the DragLinearLayout layout in the libraries folder once the gradle is synced.

注意 :同步Gradle后,您可以在library文件夹中查看DragLinearLayout布局的完整源代码。

By default the child views inside the LinearLayout won’t be draggable. For that we’ll call the method
setViewDraggable(View, View) for each ChildView.

默认情况下,LinearLayout中的子视图不可拖动。 为此,我们将调用方法
每个ChildView的setViewDraggable(View, View)

Android拖放示例项目结构 (Android Drag Drop Example Project Structure)

This android drag drop project consists of an activity and its layout along with a drawable image used in the layout. In this tutorial we’ve added the attributes of the UI widgets under styles.xml.

这个android拖放项目由一个活动及其布局以及在布局中使用的可绘制图像组成。 在本教程中,我们将UI小部件的属性添加到styles.xml下。

Android拖放代码 (Android Drag Drop Code)

The activity_main.xml code is given below:

下面给出了activity_main.xml代码:

<com.jmedeisis.draglinearlayout.DragLinearLayoutxmlns:android="https://schemas.android.com/apk/res/android"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:text="Please Drag and Drop Me Somewhere"android:layout_width="match_parent"android:layout_height="wrap_content"style="@style/TextViewStyle" /><TextViewandroid:background="@android:color/holo_red_light"android:layout_width="match_parent"android:layout_height="wrap_content"style="@style/TextViewStyle" /><TextViewandroid:text="This tutorial uses a third party library to drag LinearLayouts"android:layout_width="match_parent"android:layout_height="wrap_content"style="@style/TextViewStyle" /><ImageViewandroid:layout_width="match_parent"android:layout_height="120dp"android:scaleType="centerCrop"android:src="@drawable/img_1"/><TextViewandroid:text="Ever thought how dragging songs in an application playlist works!"android:layout_width="match_parent"android:layout_height="wrap_content"style="@style/TextViewStyle" /></com.jmedeisis.draglinearlayout.DragLinearLayout>

The above layout consists of multiple TextViews and an ImageView. These are the ChildViews of the draggable layout.

上面的布局由多个TextView和一个ImageView组成。 这些是可拖动布局的ChildViews。

The MainActivity.java is given below:

MainActivity.java如下所示:

package com.journaldev.draggablelinearlayout;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;import com.jmedeisis.draglinearlayout.DragLinearLayout;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);DragLinearLayout dragLinearLayout = (DragLinearLayout) findViewById(R.id.container);// set all children draggable except the first (the header)for(int i = 0; i < dragLinearLayout.getChildCount(); i++){View child = dragLinearLayout.getChildAt(i);dragLinearLayout.setViewDraggable(child, child); // the child is its own drag handle}}
}

In the above code we’ve explicitly set each ChildView as a draggable one. The DraggableLinearLayout class present in the third party library contains the drag and drop implementation.

在上面的代码中,我们已将每个ChildView显式设置为可拖动的一个。 第三方库中存在的DraggableLinearLayout类包含拖放实现。

Note: This library comes with the limitation that it can only support Vertical Orientation of the LinearLayout.

注意 :该库具有只能支持LinearLayout的垂直方向的限制。

The output below shows the app in action.

下面的输出显示了正在运行的应用程序。

This brings an end to android drag drop example. You can download the final Android DragLinearLayout Project from the below link.

这结束了android拖放示例。 您可以从下面的链接下载最终的Android DragLinearLayout项目

Download Android Drag Drop DraggableLinearLayout Project下载Android Drag Drop DraggableLinearLayout项目

Reference: GitHub Project

参考: GitHub项目

翻译自: https://www.journaldev.com/10200/android-drag-drop-draglinearlayout

Android拖放– DragLinearLayout相关推荐

  1. android图片拖动放大_Android拖放

    android图片拖动放大 In this tutorial, we'll be implementing Android Drag and Drop functionality in our app ...

  2. android 拖动 点击事件,Android事件详解——拖放事件DragEvent

    1.Android拖放框架的作用? 利用Android的拖放框架,可以让用户用拖放手势把一个View中的数据移到当前layout内的另一个View中去. 2.拖放框架的内容? 1)拖放事件类 2)拖放 ...

  3. android教程视频教程_Android拖放教程

    android教程视频教程 这篇文章将介绍如何在Android应用程序中实现拖放. (我目前使用的是sdk的4.0版,但最初是使用3.1版编写的. 为什么要使用拖放 当我开始使用涉及在国际象棋棋盘上移 ...

  4. android 拖动数字选择,拖放android 3.x导致illegalStateException之后的小数字拖动

    android 3.x上的拖放机制有问题: 在做一些拖动(例如30个拖动)之后出现异常(见附件链接) android技术人员在那里回答说它是API中的错误,并且说避免这个问题的唯一方法是调用垃圾收集器 ...

  5. Android 用户界面---拖放(Drag and Drop)(二)

    拖拽事件监听器和回调方法 View对象既可以用实现View.OnDragListener接口的拖放事件监听器,也可以用View对象的onDragEvent(DragEvent)回调方法来接收拖拽事件. ...

  6. android Launcher——拖放功能深入研究

    Luancher有一个相对比较复杂的功能就是拖放功能,要深入了解launcher,深入理解拖放功能是有必要的,这篇blog,我将对launcher的拖放功能做深入的了解 1.首先直观感受什么时候开始拖 ...

  7. Android RecyclerView拖放

    In this tutorial, we'll be discussing and implementing the Drag and Drop functionality over our Recy ...

  8. sap miro_使用Miro的Android,iPhone和PSP的简单拖放视频转换

    sap miro Are you looking for a way to quickly convert a movie for your iPhone, PSP, or Android devic ...

  9. 《人人都玩开心网:Ext JS+Android+SSH整合开发Web与移动SNS》销售排名第4,发篇ExtJS的文章(拖放树结点)庆祝下

    <人人都玩开心网:Ext JS+Android+SSH整合开发Web与移动SNS>一书上架短短几天,就进入了互动网计算机类销售总排名第4的好成绩(见下图).现发篇Ext JS的技术文章庆祝 ...

最新文章

  1. one-hot encoding不是万能的,这些分类变量编码方法你值得拥有
  2. SAP MM Movement Type 503的使用
  3. IAR J-Link下载程序出现错误提示:Failed to get CPU status after 4 retries Retry?
  4. 【牛客 - 272B】Xor Path(树上操作,路径异或值)
  5. linux中命令对c文件进行编译,Linux下C语言编译基础及makefile的编写
  6. BZOJ1938: [CROATIAN2010] ALADIN
  7. linq to sql取出随机记录/多表查询/将查询出的结果生成xml
  8. cockroachdb设计翻译
  9. CSDN如何修改用户名(CSDN ID)、用户昵称以及自定义博客域名等
  10. 基于NET Core 的Nuget包制作、发布和运用流程
  11. 我是住在山里的小和尚,我把我的寺庙生活告诉大家[转载之2]
  12. 项目管理与SSM框架——Spring
  13. xlsx表格怎么筛选重复数据_怎样在excel2010中筛选出重复数据呢?
  14. Celery---手机短信异步发送
  15. Spring-boot启动出现:“if you put a @ComponentScan in the default package by mistake” 错误。
  16. 已知序列1,2,3,5,8,...,求第20项的值( C 语言)
  17. linux python 路径获取
  18. Java高级工程师面试题目汇集
  19. 基于bim技术的应用软件有?哪些提高bim工作效率的revit插件
  20. 快速梳理23种常用的设计模式

热门文章

  1. 缓存jQuery对象来提高性能
  2. redhat配置java环境
  3. jQuery获取隐藏文本域
  4. WCF开发实战系列四:使用Windows服务发布WCF服务
  5. Java程序低手之关于泛型(Generic)
  6. [转载] python中 堆heapq以及 队列queue的使用
  7. [转载] Java的访问修饰符、非访问修饰符分类详细总结以及修饰符用途总结
  8. 突然情怀就上来啦,‘闭包’ 今天咱们讲一下子
  9. canvas背景效果
  10. Java基础之IO流