53、listview、expandableListview如何选中时保持高亮?
一、listView被选中后保持高亮
|
70down voteaccepted
|
To hold the color of listview item when you press it, include the following line in your listview layout:
Then define bg_key.xml in drawable folder like this: <?xml version="1.0" encoding="utf-8" ?> Finally, include this in your listview onClickListener: listView.setOnItemClickListener(new OnItemClickListener() {
This way, only one item will be color-selected at any time. You can define your color values in res/values/colors.xml with something like this: <?xml version="1.0" encoding="utf-8"?> |
二、expandableListview被选中后保持高亮
1、在expandableListview的adapter中的childList中的类,加一个变量,用来标记是否被选择
public class SettingTitleComponent {
public int set_for_what = 0;//设置什么
public int icon_id = 0;//图标id
public String title = "";//设置项标题
public String title_hint = "";//设置项提示
public boolean flagSelected = false;//用来记录是否被选择
}
2、在expandableListview对应的adapter中写一个方法,用来初始化所有“是否被选择”的标记位
public void initFlagSelected() {
for (int t = 0; t < childList.size(); t++) {
for (int k = 0; k < childList.get(t).size(); k++) {
childList.get(t).get(k).flagSelected = false;
}
}
}
3、在activity中设置expandableListview的回调
expLv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
final SettingTitleComponent stc = childList.get(groupPosition).get(childPosition);
expLvAdapter.initFlagSelected();//初始化
stc.flagSelected = true//设置被选择
expLvAdapter.notifyDataSetChanged()//更新adapter对应的列表,这里很关键,请看下一步
return true; } });
4、刷新adapter时,根据标识为去判断是否应该高亮
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub final SettingTitleComponent stc = childList.get(groupPosition).get(childPosition); CommonUtils.LogWuwei(tag, "stc title is " + stc.title + " childPosition is " + childPosition + " groupPosition is " + groupPosition); LayoutInflater inflater = (LayoutInflater) ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View grid = inflater.inflate(R.layout.relativelayout_setting_title, null); ImageView ivIcon = (ImageView) grid.findViewById(R.id.imageview_setting_title_icon);
TextView tvTitle = (TextView) grid.findViewById(R.id.textview_setting_title_text);
TextView tvHint = (TextView) grid.findViewById(R.id.textview_setting_title_hint); ivIcon.setBackgroundResource(stc.icon_id);
tvTitle.setText(stc.title);
tvHint.setText(stc.title_hint); if (stc.flagSelected) {
grid.setBackgroundColor(ctxt.getResources().getColor(R.color.Blue));
} else {
grid.setBackgroundColor(ctxt.getResources().getColor(R.color.Constrast));
}
return grid;
}
三、listview保持上次的位置
/****从这里开始记录位置信息***/
int index = listviewAllProduct.getFirstVisiblePosition();
View v = listviewAllProduct.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - listviewAllProduct.getPaddingTop());
/****记录结束***/ listviewAllProcutAdapter = new ListviewStockPlanProductAdapter(listStoreProduct, ctxt, mUiHandler);
listviewAllProduct.setAdapter(listviewAllProcutAdapter);
listviewAllProcutAdapter.notifyDataSetChanged(); /********恢复位置**********/
listviewAllProduct.setSelectionFromTop(index, top);
53、listview、expandableListview如何选中时保持高亮?的更多相关文章
- 改变listview中item选中时文字的颜色
摘要 当listview的某个item选中时,默认有个选中的高亮显示,如果你要自定义选中时的高亮显示效果,可以在listview中设置属性 android:listSelector="@dr ...
- iOS UITableView 移除单元格选中时的高亮状态
在处理UITableView表格时,我们希望用户能够和触摸单元格式进行交互,但是希望用户在完成交互之后,这些单元格的选中状态能够消失,.Cocoa Touch 提供了两种方法来防止单元格背持久选中. ...
- listview更改选中时item背景色(转)
默认情况下使用ListView背景色是黑色,选中item的高亮颜色是菊黄色,很多时候不得不自己定义背景色或者背景图 android:cacheColorHint="@android:colo ...
- vs 2012 设置选中的引用高亮 颜色
一个简单的小技巧,不用插件也可以做到,vs 2012 设置选中的引用高亮 颜色 .vs 默认的单击引用的变量时,显示的颜色是灰色,基本看不出来,所有特意抽空捣鼓了下 还真让我给找出来 工具==> ...
- 设置 TabBarItem 选中时的图片及文字颜色
TabBarController 是在 ios 开发过程中使用较为频繁的一个 Controller,但是在使用过程中经常会遇到一些问题,例如本文所要解决的,如何修改 TabBar 选中时文字及图片的颜 ...
- 转 Android RadioButton设置选中时文字和背景颜色同时改变
主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿. 搜了一下,效果和这个文章一致.转了. 原文地址:http:// ...
- IOS - UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte
1.系统默认的颜色设置 [cpp] view plaincopy //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 ...
- UITableViewCell的选中时的颜色设置
转自:http://hi.baidu.com/zhu410289616/item/0de0262910886011097508c2 1.系统默认的颜色设置 //无色 cell.selectionSty ...
- IOS中设置cell的背景view和选中时的背景view 、设置cell最右边的指示器(比如箭头\文本标签)
一.Cell的设置 1.设置cell的背景view和选中时的背景view UIImageView *bg = [[UIImageView alloc] init]; bg.image = [UIIma ...
随机推荐
- Cocos2d-x v3.1 安装图文教程(二)
Cocos2d-x v3.1 安装图文教程(二) 如果我们需要在Android平台上运行就必须安装android的SDK,如果我们只想在window上运行就只需要安装Cocos2d-x就行了.当 ...
- Android商城开发系列(十一)—— 首页秒杀布局实现
首页秒杀布局如下图: 布局使用的是LinearLayout和RecyclerView去实现,新建seckkill_item.xml,代码如下所示: <?xml version="1.0 ...
- POJ2112 Optimal Milking---二分+Floyd+网络流
题目链接: https://vjudge.net/problem/POJ-2112 题目大意: k个机器,每个机器最多服务m头牛. c头牛,每个牛需要1台机器来服务. 告诉你牛与机器每个之间的直接距离 ...
- iOS开发学习之大牛们的博客
http://blog.csdn.net/iosbird/article/details/51981023 唐巧:http://blog.devtang.com/blog/archives/ 王巍:h ...
- SCSI add-single-device and remove-single-device
众所周知,SATA和SCSI是支持热插拔的,但是新装了这类支持热插拔的驱动器,系统不会马上识别的,往往我们需要重启系统来识别,但是有另外一种方法可以很方面的让系统识别新的设备. 作为系统管理员,需要了 ...
- cuda api查询问题
在查询CUDA运行时API的时候,我用360极速浏览器的时候搜索结果一直不出来,但是用火狐的话就很流畅,所以建议大家在开发时还是用火狐浏览器.
- C#分块拷贝大文件
//定义源文件和目标文件,绝对路径 public static string source = @"E:\C#\C#编程语言详解.pdf"; //2014-6-10 Trainin ...
- 国产中标麒麟Linux部署dotnet core 环境并运行项目 (一) 安装dotnet core
背景 根据我之前写的文章 将 Net 项目升级 Core项目经验:(一)迁移Net项目为Net Core\Standard项目,我们将公司内部最核心的ORM框架迁移到net core 上面,并在win ...
- 深入理解Java GC
一.概述 GC(Carbage Collection)垃圾收集器,由JVM自动回收已死亡的对象垃圾. 这也是Java与C++等语言的主要区别之一. 二.如何确认对象已死 1. 引用计数算法 引用计数法 ...
- JavaScript 遍历对象查找指定的值并返回路径
问:JavaScript 如何查找对象中某个 value 并返回路径上所有的 key? let obj = { key1: 'str1', key2: { key3: 'str3' }, key4: ...