android gridview佈局,實現長按某一個,全部項都顯示刪除的圖標

最近一直忙着項目開發,有段時間沒有寫博文了,今天想跟你們分享的是長按gridview中的某一項顯示刪除圖標,此時點擊某項即可刪除,再長按取消刪除圖標。java

gridview的佈局文件以下:android

<?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;
 }

寫到這裏,但願能給你們帶來一些幫助!!!!!!!! ide

相關文章
相關標籤/搜索