Android DynamicGrid是一个第三方开源项目,DynamicGrid在github上的项目主页是:DynamicGrid
它实现在一个网格布局内,拖曳任意子view实现动态的交换位置,这很类似手机的桌面,手机桌面的图标,均可自由拖曳实现摆放位置的交换,如动图所示:
DynamicGrid使用起来简单,需要注意:
(1)最好使用完全的BaseDynamicGridAdapter构造BaseDynamicGridAdapter构造函数,否则可能无法正常工作。
(2)通常以长按模式触发拖曳交换位置操作,所以一般在onItemLongClick里面startEditMode开始编辑。
(3)按返回键取消拖曳操作,代码比较固定,重写onBackPressed:
1
2
3
4
5
6
7
8
9
10
|
@Override public void onBackPressed() { //此处将取消拖曳编辑模式,取消方式为按返回键。 if (gridView.isEditMode()) { gridView.stopEditMode(); } else { super .onBackPressed(); } } |
给出一个完整的代码例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
package zhangphil.app; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.TextView; import android.widget.Toast; import org.askerov.dynamicgrid.BaseDynamicGridAdapter; import org.askerov.dynamicgrid.DynamicGridView; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { private DynamicGridView gridView; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (DynamicGridView) findViewById(R.id.dynamicgrid); ArrayList lists = new ArrayList(); for ( int i = 0 ; i < 100 ; i++) { lists.add( "项目-" + i); } //最好以完全方式构造,否则可能无法正常工作 MyAdapter adapter = new MyAdapter( this , lists, 3 ); gridView.setAdapter(adapter); gridView.setOnItemLongClickListener( new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { //长按触发拖曳交换位置操作。 gridView.startEditMode(position); return true ; } }); gridView.setOnDragListener( new DynamicGridView.OnDragListener() { @Override public void onDragStarted( int position) { //Toast.makeText(getApplication(), "drag started at position " + position,Toast.LENGTH_SHORT).show(); } @Override public void onDragPositionsChanged( int oldPosition, int newPosition) { Toast.makeText(getApplication(), String.format( "from %d to %d" , oldPosition, newPosition), Toast.LENGTH_SHORT).show(); } }); } @Override public void onBackPressed() { //此处将取消拖曳编辑模式,取消方式为按返回键。 if (gridView.isEditMode()) { gridView.stopEditMode(); } else { super .onBackPressed(); } } private class MyAdapter extends BaseDynamicGridAdapter { @Override public View getView( int i, View view, ViewGroup viewGroup) { View v = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, null ); TextView text = (TextView) v.findViewById(android.R.id.text1); text.setText( "项目-" + i); v.setPadding( 10 , 10 , 10 , 10 ); return v; } public MyAdapter(Context context, List<?> items, int columnCount) { super (context, items, columnCount); } } } |
布局文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "match_parent" android:layout_height = "match_parent" > < org.askerov.dynamicgrid.DynamicGridView android:id = "@+id/dynamicgrid" android:layout_height = "wrap_content" android:layout_width = "match_parent" android:numColumns = "3" /> </ LinearLayout > |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/zhangphil/article/details/51602008