展会信息港展会大全

长按图标显示删除图标 android开发之gridview布局
来源:互联网   发布日期:2015-10-13 14:58:02   浏览:2840次  

导读:长按gridview中的某一项显示删除图标,此时点击某项便可删除,再长按取消删除图标。gridview的布局文件如下:?xml version=1 0 encoding=utf-8?LinearLayout xmlns:android=http: schemas android c ...

长按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

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港