18
18
-----
19
19
Add it to your project using Gradle:
20
20
21
- compile 'com.jmedeisis:draglinearlayout:1.0.1'
21
+ ``` groovy
22
+ compile 'com.jmedeisis:draglinearlayout:1.0.1'
23
+ ```
22
24
23
25
The ` DragLinearLayout ` can be used in place of any ` LinearLayout ` . However, by default, children
24
26
will not be draggable. To set an existing ` View ` as draggable, use
@@ -27,60 +29,68 @@ will not be draggable. To set an existing `View` as draggable, use
27
29
28
30
XML layout file:
29
31
30
- <com.jmedeisis.draglinearlayout.DragLinearLayout
31
- xmlns:android="http://schemas.android.com/apk/res/android"
32
- android:id="@+id/container"
32
+ ``` xml
33
+ <com .jmedeisis.draglinearlayout.DragLinearLayout
34
+ xmlns : android =" http://schemas.android.com/apk/res/android"
35
+ android : id =" @+id/container"
36
+ android : layout_width =" match_parent"
37
+ android : layout_height =" match_parent" >
38
+
39
+ <TextView
40
+ android : layout_width =" match_parent"
41
+ android : layout_height =" wrap_content"
42
+ android : text =" @string/text" />
43
+
44
+ <ImageView
45
+ android : layout_width =" match_parent"
46
+ android : layout_height =" 120dp"
47
+ android : scaleType =" centerCrop"
48
+ android : src =" @drawable/image" />
49
+
50
+ <Button
51
+ android : id =" @+id/button"
33
52
android : layout_width =" match_parent"
34
- android:layout_height="match_parent" >
35
-
36
- <TextView
37
- android:layout_width="match_parent"
38
- android:layout_height="wrap_content"
39
- android:text="@string/text" />
40
-
41
- <ImageView
42
- android:layout_width="match_parent"
43
- android:layout_height="120dp"
44
- android:scaleType="centerCrop"
45
- android:src="@drawable/image"/>
46
-
47
- <Button
48
- android:id="@+id/button"
49
- android:layout_width="match_parent"
50
- android:layout_height="wrap_content"
51
- android:text="@string/button_text"/>
52
-
53
- </com.jmedeisis.draglinearlayout.DragLinearLayout>
54
-
53
+ android : layout_height =" wrap_content"
54
+ android : text =" @string/button_text" />
55
+
56
+ </com .jmedeisis.draglinearlayout.DragLinearLayout>
57
+ ```
58
+
55
59
Enabling drag & swap for all child views:
56
60
57
- DragLinearLayout dragLinearLayout = (DragLinearLayout) findViewById(R.id.container);
58
- for(int i = 0; i < dragLinearLayout.getChildCount(); i++){
59
- View child = dragLinearLayout.getChildAt(i);
60
- // the child will act as its own drag handle
61
- dragLinearLayout.setViewDraggable(child, child);
62
- }
61
+ ``` java
62
+ DragLinearLayout dragLinearLayout = (DragLinearLayout ) findViewById(R . id. container);
63
+ for (int i = 0 ; i < dragLinearLayout. getChildCount(); i++ ){
64
+ View child = dragLinearLayout. getChildAt(i);
65
+ // the child will act as its own drag handle
66
+ dragLinearLayout. setViewDraggable(child, child);
67
+ }
68
+ ```
63
69
64
70
Use ` #addDragView(View, View) ` ,` #addDragView(View, View, int) ` and ` #removeDragView(View) ` to
65
71
manage draggable children dynamically:
66
72
67
- final View view = View.inflate(context, R.layout.view_layout, null);
68
- dragLinearLayout.addDragView(view, view.findViewById(R.id.view_drag_handle));
69
-
70
- // ..
71
-
72
- dragLinearLayout.removeDragView(view);
73
+ ``` java
74
+ final View view = View . inflate(context, R . layout. view_layout, null );
75
+ dragLinearLayout. addDragView(view, view. findViewById(R . id. view_drag_handle));
76
+
77
+ // ..
78
+
79
+ dragLinearLayout. removeDragView(view);
80
+ ```
73
81
74
82
Attach an ` OnViewSwapListener ` with ` #setOnViewSwapListener(OnViewSwapListener) ` to detect changes
75
83
to the ordering of child ` View ` s:
76
84
77
- dragLinearLayout.setOnViewSwapListener(new DragLinearLayout.OnViewSwapListener() {
78
- @Override
79
- public void onSwap(View firstView, int firstPosition,
80
- View secondView, int secondPosition) {
81
- // update data, etc..
82
- }
83
- });
85
+ ``` java
86
+ dragLinearLayout. setOnViewSwapListener(new DragLinearLayout .OnViewSwapListener () {
87
+ @Override
88
+ public void onSwap (View firstView , int firstPosition ,
89
+ View secondView , int secondPosition ) {
90
+ // update data, etc..
91
+ }
92
+ });
93
+ ```
84
94
85
95
When placing the ` DragLinearLayout ` inside a ` ScrollView ` , call ` #setContainerScrollView(ScrollView) `
86
96
to enable the user to scroll while dragging a child view.
0 commit comments