最近一直忙着项目开发,有段时间没有写博文了,今天想跟大家分享的是长按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;
}

android gridview布局,实现长按某一个,所有项都显示删除的图标的更多相关文章

  1. Android自定义控件(四)——让每一个Activity UI都具有弹性

    前面我们已经介绍了如何让你的ScrollView,ListView具有弹性, 今天,我们在前面的基础上,做一下适当的修改,让那些既不是ScrollView,也不是ListView的Activity页面 ...

  2. Android开发自学笔记(Android Studio1.3.1)—2.开始第一个Android应用

    一.前言      使用Android Studio开发Android应用是一件非常简单的事情,因为它会帮你自动完成很多工作.本篇我们主要完成一个单击按钮在文本框显示当前时间的简单应用,借此来演示一下 ...

  3. Android线性布局(Linear Layout)

    Android线性布局(Linear Layout) LinearLayout是一个view组(view group),其包含的所有子view都以一个方向排列,垂直或是水平方向.我们能够用androi ...

  4. Android Animation动画实战(一): 从布局动画引入ListView滑动时,每一Item项的显示动画

    前言: 之前,我已经写了两篇博文,给大家介绍了Android的基础动画是如何实现的,如果还不清楚的,可以点击查看:Android Animation动画详解(一): 补间动画 及 Android An ...

  5. Android layout 布局 属性详解

    第一类:属性值 true或者 false           android:layout_centerHrizontal 水平居中     android:layout_centerVertical ...

  6. Android layout布局属性、标签属性总结大全

    RelativeLayout 第一类:属性值为true可false android:layout_centerHrizontal        水平居中 android:layout_centerVe ...

  7. Java-android使用GridView布局的电子相册&服务器获取图片

    转  http://www.tuicool.com/articles/B7JNv2 电子相册的思路: 1.先是考虑布局,我用的是GridView布局 2.GridView中又该怎么显示图片,其实我的这 ...

  8. Android常用布局和控件

    一.Android常用布局属性 1. LinearLayout的特有属性 android:orientation:设置布局排列方式   android:layout_weight:设置所占布局的权重  ...

  9. Android GridView 通过seletor 设置状态和默认状态

    Android中可以通过selector控制GridView Item 的状态,而省去使用代码控制 GridView View Selector Xml文件 <?xml version=&quo ...

随机推荐

  1. QMessageBox 中的 OK 按钮改为中文“确定”

    有很多资料用于将 QMessageBox 的 OK 改为中文.但大多很麻烦.本文提供一个简便方法,用于定制 QMessageBox 的按钮,包括将其翻译成中文显示.   QMessageBox  对其 ...

  2. Raspberrypi安装使用开发简要说明

    Raspberrypi安装使用开发简要说明 (更新于2013年8月25日 newsuppy) 一,安装 使用win32diskimager将操作系统的image刷在SD卡上,image文件可以在htt ...

  3. cf446A DZY Loves Sequences

    A. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. DBA 经典面试题(5)

    国外公司的Oracle DBA试题 Oracle DBA Interview Questions 1. How many memory layers are in the shared pool? 2 ...

  5. [Linux] killall 、kill 、pkill 命令详解

    killall 命令 Linux系统中的killall命令用于杀死指定名字的进程(kill processes by name).我们可以使用kill命令杀死指定进程PID的进程,如果要找到我们需要杀 ...

  6. 格而知之16:我所理解的Block(2)

    11.那么Block到底是怎么实现的呢?试一试通过将Block 的代码转换成普通C语言代码来查看它的实现过程. 要将OC代码转换成C语言代码,可以使用clang编译的一个命令: 通过这个命令能把指定文 ...

  7. Non-negative Partial Sums(单调队列)

    Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  8. SVG Loading

    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="64&qu ...

  9. JS 逗号表达式

    JavaScript中逗号运算符 JavaScript中逗号运算符(,)是顺序执行两个表达式.使用方法: expression1, expression2 其中expression1是任何表达式.ex ...

  10. python re 正则

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...