最近一直忙着项目开发,有段时间没有写博文了,今天想跟大家分享的是长按gridview中的某一项显示删除图标,此时点击某项便可删除,再长按取消删除图标。
gridview的布局文件如下:
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
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:id
=
"@+id/ll_grid_item"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:gravity
=
"center"
android:orientation
=
"vertical"
>
<
FrameLayout
android:id
=
"@+id/starred_item_layout"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:layout_gravity
=
"center"
>
<
LinearLayout
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:background
=
"@drawable/bg_btn_selector_deny"
android:gravity
=
"center"
android:orientation
=
"vertical"
android:layout_marginTop
=
"4dip"
android:layout_marginRight
=
"4dip"
>
<
ImageView
android:id
=
"@+id/img"
android:layout_width
=
"60dip"
android:layout_height
=
"55dip"
/>
<
TextView
android:id
=
"@+id/name_tv"
android:layout_width
=
"70dip"
android:layout_height
=
"wrap_content"
android:layout_marginTop
=
"10dip"
android:textColor
=
"@android:color/black"
android:textSize
=
"15sp"
android:textStyle
=
"bold"
android:gravity
=
"center"
/>
</
LinearLayout
>
<
ImageView
android:id
=
"@+id/delete_markView"
android:layout_width
=
"20dip"
android:layout_height
=
"20dip"
android:adjustViewBounds
=
"true"
android:layout_gravity
=
"right|top"
android:visibility
=
"gone"
android:src
=
"@drawable/delete"
/>
</
FrameLayout
>
</
LinearLayout
>
|
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
|
gridview的adapter如下:
public
class
GridViewAdapter
extends
BaseAdapter{
private
String names[];
private
int
icons[];
private
Context mContext;
private
TextView name_tv;
private
ImageView img;
private
View deleteView;
private
boolean
isShowDelete;
//根据这个变量来判断是否显示删除图标,true是显示,false是不显示
public
FragmentGridViewAdapter(Context mContext,String names[],
int
icons[]) {
this
.mContext = mContext;
this
.names=names;
this
.icons=icons;
}
public
void
setIsShowDelete(
boolean
isShowDelete){
this
.isShowDelete=isShowDelete;
notifyDataSetChanged();
}
@Override
public
int
getCount() {
return
icons.length;
}
@Override
public
Object getItem(
int
position) {
// TODO Auto-generated method stub
return
icons[position];
}
@Override
public
long
getItemId(
int
position) {
// TODO Auto-generated method stub
return
position;
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.fragmet_grid_item,
null
);
img = (ImageView) convertView.findViewById(R.id.img);
name_tv = (TextView) convertView.findViewById(R.id.name_tv);
deleteView = convertView.findViewById(R.id.delete_markView);
deleteView.setVisibility(isShowDelete?View.VISIBLE:View.GONE);
//设置删除按钮是否显示
img.setBackgroundResource(icons[position]);
name_tv.setText(names[position]);
return
convertView;
}
}
看到这里大家是否觉得很简单呢,接下来,我们就可以在长按方法里来设置isShowDelete的值了
@Override
public
boolean
onItemLongClick(AdapterView<?> parent, View view,
int
position,
long
id) {
if
(isShowDelete) {
isShowDelete =
false
;
}
else
{
isShowDelete =
true
;
}
mGridAdapter.setIsShowDelete(isShowDelete);
return
true
;
}
|
写到这里,希望能给大家带来一些帮助!!!!!!!!
转载地址:http://my.oschina.net/u/1262457/blog/159885?fromerr=ycmT30XO