달력

4

« 2024/4 »

  • 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
728x90
반응형

안드로이드 - drag & drop으로 아이템 이동시키는 GridView

DragDropGridView.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
public class DragDropGridView extends GridView
{
private Context _context;
private int _selectItemIndex = -1;
private ImageView _dragView;
private WindowManager _windowManager;
private WindowManager.LayoutParams _windowParams;
private Point _touchPosition = new Point(0, 0);
private Point _touchPositionOffset = new Point(0, 0);
private OnDropListener _onDropListener;
public DragDropGridView(Context $context, AttributeSet $attrs, int $defStyle)
{
super($context, $attrs, $defStyle);
init($context);
}
public DragDropGridView(Context $context, AttributeSet $attrs)
{
super($context, $attrs);
init($context);
}
public DragDropGridView(Context $context)
{
super($context);
init($context);
}
private void init(Context $context)
{
_context = $context;
setOnItemLongClickListener(onItemLongClickListener);
_windowManager = (WindowManager) $context.getSystemService(Context.WINDOW_SERVICE);
_windowParams = new WindowManager.LayoutParams();
_windowParams.gravity = Gravity.LEFT | Gravity.TOP;
_windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
_windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
_windowParams.format = PixelFormat.TRANSLUCENT;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent $e)
{
int x = (int) $e.getX();
int y = (int) $e.getY();
switch ($e.getAction())
{
case MotionEvent.ACTION_DOWN:
_touchPosition.set(x, y);
break;
}
return super.onInterceptTouchEvent($e);
}
@Override
public boolean onTouchEvent(MotionEvent $e)
{
int x = (int) $e.getX();
int y = (int) $e.getY();
switch ($e.getAction())
{
case MotionEvent.ACTION_MOVE:
doDragView(x, y);
break;
case MotionEvent.ACTION_UP:
doDropView(x, y);
break;
case MotionEvent.ACTION_CANCEL:
setNullDragView();
break;
default:
break;
}
return super.onTouchEvent($e);
}
// 드래그 하는 동안 보일 view
private void doMakeDragview()
{
View item = (View) getChildAt(_selectItemIndex - getFirstVisiblePosition()); // getFirstVisiblePosition() 이 없으면 스크롤 됐을 때 문제 생김
item.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());
ImageView image = new ImageView(_context);
image.setBackgroundColor(Color.parseColor("#ff0000"));
image.setImageBitmap(bitmap);
_touchPositionOffset.x = (int) (item.getWidth() * 0.5);
_touchPositionOffset.y = (int) (item.getHeight() * 0.5);
_windowParams.x = _touchPosition.x - _touchPositionOffset.x;
_windowParams.y = _touchPosition.y - _touchPositionOffset.y;
_windowManager.addView(image, _windowParams);
_dragView = image;
}
// 드래그하기
private void doDragView(int $x, int $y)
{
if (_dragView == null)
return;
_windowParams.x = $x - _touchPositionOffset.x;
_windowParams.y = $y - _touchPositionOffset.y;
_windowManager.updateViewLayout(_dragView, _windowParams);
}
// 드랍
private void doDropView(int $x, int $y)
{
if (_dragView == null)
return;
int toIndex = pointToPosition($x, $y);
if (toIndex <= INVALID_POSITION)
{
setNullDragView();
return;
}
_onDropListener.drop(_selectItemIndex, toIndex);
setNullDragView();
}
// 드래그 뷰 지우기
private void setNullDragView()
{
if (_dragView != null)
{
_windowManager.removeView(_dragView);
_dragView = null;
}
}
/**
* 드랍 후에 실행 할 리스터
* @param $listener
*/
public void setOnDropListener(OnDropListener $listener)
{
_onDropListener = $listener;
}
/****************
* Listener
***************/
OnItemLongClickListener onItemLongClickListener = new OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int $position, long arg3)
{
if ($position <= INVALID_POSITION)
return true;
_selectItemIndex = $position;
doMakeDragview();
return true;
}
};
/************
* 인터페이스
************/
public interface OnDragListener
{
void drag(int from, int to);
}
public interface OnDropListener
{
void drop(int from, int to);
}
}



AppListAdapter.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
public class AppListAdapter extends BaseAdapter
{
private ArrayList<applistitem> _items = new ArrayList<applistitem>();
private LayoutInflater _inflater;
public AppListAdapter(Context $context)
{
_inflater = LayoutInflater.from($context);
}
/**
* 항목 추가
* @param $id 고유아이디
* @param $icon 아이콘 drawable
* @param $title 앱 이름
*/
public void addItem(CharSequence $id, Drawable $icon, CharSequence $title)
{
AppListItem item = new AppListItem(getCount(), $id, $icon, $title);
_items.add(item);
}
/**
* 항목 추가
* @param $id 고유아이디
* @param $icon 아이콘 리소스 id
* @param $title 앱 이름
*/
public void addItem(CharSequence $id, int $icon, CharSequence $title)
{
AppListItem item = new AppListItem(getCount(), $id, $icon, $title);
_items.add(item);
}
/**
* 항목 추가
* @param $id 고유아이디
* @param $icon 아이콘 bitmap
* @param $title 앱 이름
*/
public void addItem(CharSequence $id, Bitmap $icon, CharSequence $title)
{
AppListItem item = new AppListItem(getCount(), $id, $icon, $title);
_items.add(item);
}
/**
* 특정 위치에 아이템 넣기
* @param $index
* @param $item
*/
public void addItemAt(int $index, AppListItem $item)
{
_items.add($index, $item);
}
/**
* 아이콘 변경
* @param $index 위치
* @param $icon 아이콘 drawable
*/
public void setItemIcon(int $index, Drawable $icon)
{
_items.get($index)._icon = $icon;
notifyDataSetChanged();
}
/**
* 특정 위치의 아이템 지우기
* @param $index
*/
public AppListItem removeItemAt(int $index)
{
return _items.remove($index);
}
/**
* 항목들 전부 지우기
*/
public void clear()
{
_items.clear();
}
@Override
public int getCount()
{
return _items.size();
}
@Override
public Object getItem(int $index)
{
return _items.get($index);
}
@Override
public long getItemId(int $index)
{
return _items.get($index)._id;
}
/**
* 항목의 고유아이디
* @param $index 선택된 행
* @return
*/
public CharSequence getItemAuId(int $index)
{
return _items.get($index)._auid;
}
/**
* 앱 이름 가져오기
* @param $index
* @return
*/
public CharSequence getItemTitle(int $index)
{
return _items.get($index)._title;
}
/**
* 아이콘 가져오기
* @param $index
* @return
*/
public Drawable getItemIcon(int $index)
{
return _items.get($index)._icon;
}
@Override
public View getView(int $index, View $convertView, ViewGroup $parent)
{
View view = _inflater.inflate(R.layout.list_item_app_list, null);
ImageView icon = (ImageView) view.findViewById(R.id.item_icon);
icon.setImageDrawable(_items.get($index)._icon);
TextView title = (TextView) view.findViewById(R.id.item_title);
title.setText(_items.get($index)._title);
return view;
}
}



AppListItem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class AppListItem
{
long _id;
CharSequence _auid;
Drawable _icon;
CharSequence _title;
public AppListItem(int $id, CharSequence $auid, Drawable $icon, CharSequence $title)
{
_auid = $auid;
_icon = $icon;
_title = $title;
}
public AppListItem(int $id, CharSequence $auid, Bitmap $icon, CharSequence $title)
{
_auid = $auid;
_icon = (Drawable) new BitmapDrawable($icon);
_title = $title;
}
}



list_item_app_list.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>



AppListActivity.java
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
public class AppListActivity extends Activity
{
private ArrayList<string> data = new ArrayList<string>();
private DragDropGridView _itemList;
protected AppListAdapter _adapter;
protected int _type;
private int _icon_iterator;
public AppListActivity()
{
for (int i = 0; i < 10; i++)
{
data.add(i + " 호호호");
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.act_app_list);
_itemList = (DragDropGridView) findViewById(R.id.list_apps);
_adapter = new AppListAdapter(this);
for (String s : data)
_adapter.addItem(1, R.drawable.ic_icon, s);
_itemList.setAdapter(_adapter);
_itemList.setOnDropListener(onDropListener);
}
/*************************
* Listener
************************/
// 아이템 이동 후 손을 뗐을 때
private OnDropListener onDropListener = new OnDropListener()
{
@Override
public void drop(int from, int to)
{
AppListItem item = _adapter.removeItemAt(from);
_adapter.addItemAt(to, item);
_adapter.notifyDataSetChanged();
}
};
}



act_app_list.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.test.Views.DragDropGridView
android:id="@+id/list_apps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:columnWidth="80dp"/>
</LinearLayout>
728x90
반응형
:
Posted by mapagilove