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.DragLinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Please Drag and Drop Me Somewhere"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextViewStyle" />
<TextView
android:background="@android:color/holo_red_light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextViewStyle" />
<TextView
android:text="This tutorial uses a third party library to drag LinearLayouts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextViewStyle" />
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:scaleType="centerCrop"
android:src="@drawable/img_1"
/>
<TextView
android: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 {
@Override
protected 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.
![android drag drop example, android DragLinearLayout](https://cdn.journaldev.com/wp-content/uploads/2016/01/draggable_android_output.gif)
下面的输出显示了正在运行的应用程序。
This brings an end to android drag drop example. You can download the final Android DragLinearLayout Project from the below link.
这结束了android拖放示例。 您可以从下面的链接下载最终的Android DragLinearLayout项目 。
Reference: GitHub Project
参考: GitHub项目
翻译自: https://www.journaldev.com/10200/android-drag-drop-draglinearlayout