• android gridview布局,实现长按某一个,所有项都显示删除的图标


    最近一直忙着项目开发,有段时间没有写博文了,今天想跟大家分享的是长按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;
     }
  • 相关阅读:
    RESTful API 设计最佳实践
    Linux给目录创建软链接的技巧
    KVM资源划分分配技巧
    SpringBoot无法启动,Process finished with exit code 0
    Shell中EOF内容转义
    TeamViewer运行在Windows Server 2008下连接时错误提示:正在初始化显示参数
    CentOS使用chkconfig增加开机服务提示service xxx does not support chkconfig的问题解决
    Redis哨兵模式主从同步不可以绑定127.0.0.1或者0.0.0.0,不然无法进行主从同步
    Spring Boot项目的Logback配置文件使用yaml格式
    Shell脚本里的双冒号是什么意思
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5447632.html
Copyright © 2020-2023  润新知