最近一直忙着项目开发,有段时间没有写博文了,今天想跟大家分享的是长按gridview中的某一项显示删除图标,此时点击某项便可删除,再长按取消删除图标。
gridview的布局文件如下:
<?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>
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; }
<?
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
>