Gridview 单选效果实现,且用且珍惜
Gridview 单选效果,在androidapp中实现非常频繁,我这里提供我的一种思路,这是我的思维导图:

怎么样将这样的思维导图翻译成java代码了,请看下面的代码了:
// 电子产品的图片列表信息
static class ConfigInfo {
static final int[] img = { R.drawable.electron_dvdplay,
R.drawable.electron_toudaishishexianji,
R.drawable.electron_miniyinxiang,
R.drawable.electron_weidan_camera };
}
static class ConfigInfos{
static final int[] imgs = { R.drawable.electron_dedplat_select,
R.drawable.electron_toudaishishexiangji_select,
R.drawable.electron_miniyinxiang,
R.drawable.electron_weidan_select };
}
// GridView 设配数据 class Gridadapter extends BaseAdapter { List<Electronic> listbean;
Context context;
private int lastPosition = -1; // 记录上一次选中的图片位置,-1表示未选中任何图片
private boolean multiChoose; // 表示当前适配器是否允许多选
private List<Boolean> mImage_bs = new ArrayList<Boolean>(); // 定义一个向量作为选中与否容器
private List<Integer> mSelectItems = new ArrayList<Integer>(); public Gridadapter(Context context, List<Electronic> listbean,
Boolean isMulti) {
this.context = context;
this.listbean = listbean;
multiChoose = isMulti; for (int i = 0; i < listbean.size(); i++)
mImage_bs.add(false);
} //
// private LayerDrawable makeBmp(Electronic id, boolean isChosen) {
// Bitmap mainBmp = ((BitmapDrawable) context.getResources()
// .getDrawable(id.getIcon())).getBitmap();
//
// // 根据isChosen来选取对勾的图片
// Bitmap seletedBmp;
// if (isChosen == true)
// seletedBmp = BitmapFactory.decodeResource(
// context.getResources(), R.drawable.gridview_chech_bg1);
// else
// seletedBmp = BitmapFactory.decodeResource(
// context.getResources(),
// R.drawable.electron_shuma_camera);
//
// // 产生叠加图
// Drawable[] array = new Drawable[2];
// array[0] = new BitmapDrawable(mainBmp);
// array[1] = new BitmapDrawable(seletedBmp);
// LayerDrawable la = new LayerDrawable(array);
// la.setLayerInset(50, 50, 50, 50, 50);
// la.setLayerInset(1, 10, -5, 60, 45);
//
// return la; // 返回叠加后的图
// } // public void changeState(int position) {
// // 多选时
// if (multiChoose == true) {
// mImage_bs
// .setElementAt(!mImage_bs.elementAt(position), position); // 直接取反即可
// }
// // 单选时
// else {
// if (lastPosition != -1)
// mImage_bs.setElementAt(false, lastPosition); // 取消上一次的选中状态
// mImage_bs
// .setElementAt(!mImage_bs.elementAt(position), position); // 直接取反即可
// lastPosition = position; // 记录本次选中的位置
// }
// notifyDataSetChanged(); // 通知适配器进行更新
// } @Override
public int getCount() {
return listbean.size(); } @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listbean.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final Handle handle;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.grivview_textview_img, null);
handle = new Handle();
handle.imageView = (ImageView) convertView
.findViewById(R.id.Grid_image);
handle.tv = (TextView) convertView.findViewById(R.id.grid_tv);
convertView.setTag(handle);
} else {
handle = (Handle) convertView.getTag();
}
// 设置数据
// handle.imageView.setBackgroundResource(listbean.get(position)
// .getIcon());
int resId = mImage_bs.get(position) ? ConfigInfos.imgs[position]
: ConfigInfo.img[position];
handle.imageView.setImageResource(resId);
// handle.imageView.setBackgroundDrawable(makeBmp(
// listbean.get(position), mImage_bs.get(position)));
handle.imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("图片中的项目", "你点击了按钮" + listbean.get(position)
+ position);
boolean selected=mImage_bs.get(position);
mImage_bs.set(position, !selected);
notifyDataSetChanged();
}
}); return convertView;
}
通过这么篇幅代码,我们可以得出来这样的结论:
Ⅰ用了两个实体类存储图片的实体的信息,一个布尔数组来存储是否选中的结果。
Ⅱ点击了相应的某项以后,使其该布尔数组所对应的布尔变量变成相应的非值。
这样的gridview的运行结果如下:

Gridview 单选效果实现,且用且珍惜的更多相关文章
- 利用GridView实现单选效果
1.实现如图所示的单选效果 由于Android提供的单选按钮radiobutton只能单行或单列显示,且样式并不美观,故可用GridView进行改造,实现单选效果,而要实现这样的效果重点就在GridV ...
- ios UIButton设置单选效果,以及同时设置图片和标题
一,设置单选效果 - (void)selectedBtnPress:(UIButton*)sender { //首先把原来按钮的选中效果消除 for (int i=0;i<num;i++) {/ ...
- GridView拖动效果实现
GridView拖动效果实现 1. 重新GridView控件 package com.whbs.drag.widget; import com.whbs.drag.DragGridActivit ...
- checkbox 实现单选效果(html)
note:在html <input> 标签类中的checkbox实现单选效果. 在最近的开发项目中,客户要求使用小方格子实现“单选”功能,显然圆点的radio被out了,只能选择chckb ...
- ViewPagerWithViewDemo【ViewPager和View搭配以及演示获取里面的值和CheckBox单选效果】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 简单记录下ViewPager和自定义布局view的搭配使用以及布局文件中单选效果.获取viewpager布局内部值的功能. 效果图 ...
- ios开发之--多个按钮单选效果
开发项目时,有很多场景需要用到按钮单选效果,例如充值页面,选择标签页面等,具体实现代码如下: 1,创建 -(UIView *)headerView { CGFloat width = (Kscreen ...
- [转]Gridview中实现RadioButton单选效果
HTML <asp:TemplateField ItemStyle-Width="22px"> <ItemTemplate> <asp:RadioBu ...
- css实现单选效果,看看有趣的tabIndex
以前我实现单选变色几乎都是用js实现的,今天看到有个css属性可以直接实现单选变色,很开心啊~ 话不多说看效果 实现的代码如下 下面我们看看用focus实现别的有趣的效果 话不多说看效果 实现的代码如 ...
- element-ui Table表格结合CheckBox实现单选效果
最近做项目遇到一个需求,需要实现一个表格的单选,由于项目使用的是element-ui.于是去看了表格的文档,确实有单选的方法,但是官方的单选是直接选中表格行,通过颜色来区分 看着效果不明显,实际需要一 ...
随机推荐
- hdu 4417 区间内比h小的数 划分树
二分查找最近一个比h小的数 #include<cstdio> #include<iostream> #include<algorithm> #include< ...
- css基础 行内元素 块级元素
1.行内元素(内联元素 inlineElement) 特点:不占据一行,无法设置宽高及行高,其宽度随着内容增加,高度随字体大小而改变,margin只对左右起作用,上下无效. 常见有: a - 锚点,b ...
- hdu 5774 Where Amazing Happens 水题
Where Amazing Happens 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5774 Description As the premie ...
- [原创]Linux下网络性能测试Netperf工具介绍及安装
[原创]Linux下网络性能测试Netperf工具介绍及安装 1 官方网站 http://www.netperf.org/netperf/ 2 Netperf介绍 Netperf是一种网络性能的测试工 ...
- Mysql数据库大表归档操作
由于公司的某个系统数据表过亿,考虑到数据表的压力.所以根据某个时间点对数据表做了一个归档.以下的操作是在当前的数据库新建表用于存储历史数据,然后再对生产表做一个清理操作.如果有条件的话可以把归档的数据 ...
- swap文件查看
建议 Swap 使用单独的分区: a swap file a combination of swap partitions and swap files. Swap 大小的计算公式: M 等于物理内存 ...
- Revit Family API 找到实体某一方向上的面。
PlanarFace.Normal取得向量.IsAlmostEqualTo判断向量是否一致. // ================================================== ...
- 自己动手写Vue插件Toast
<style> .vue-toast { width: 100%; height: 100%; position: fixed; top: 0px; left: 0px; backgrou ...
- Unity3D实践系列07,组件的启用或禁用开关,物体的的可见或不可见开关,以及相应事件
创建一个Unity项目. 在"Project"窗口中,在"Asserts"中,添加"_MyScene"文件夹. 点击"File&q ...
- Spring Cloud Gateway服务网关
原文:https://www.cnblogs.com/ityouknow/p/10141740.html Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gatewa ...