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 ...
随机推荐
- 一些C/C++中的函数
项目中使用到的C/C++中的一些函数,记录下来加以理解和掌握. 1.memset( ) memset是计算机中C/C++语言函数.将s所指向的某一块内存中的前n个 字节的内容全部设置为ch指定的ASC ...
- Java Web中web.xml文件简单介绍
参考博客: https://www.cnblogs.com/Y-oung/p/8401549.html 1.XML 声明和根元素 <?xml version="1.0" en ...
- 《Unity預計算即時GI》笔记:三、Clusters和总结
Clusters 叢集,透過修改叢集(Clusters)也是一個降低Unity預計算流程所需要執行的工作數量的好方法.降低叢集數量也能提高執行時的效能. 當採用PRGI來計算場景光照時,Unity會簡 ...
- JSP学习 三大指令、九个内置对象、JavaBean、EL表达式
1.page-->最复杂:<%@page language="java" info="xxx"...%> * pageEncoding和con ...
- badboy页面脚本发生错误,解决方案
1.参考网址:https://jingyan.baidu.com/article/e9fb46e17537797520f76645.html?from=qqbrowser061108 本人亲自测试,方 ...
- 为OSSIM添加 ossec的linux agent
1,安装环境 [root@node32 test]# yum groupinstall "Development Tools" -y Installed: byacc.x86_64 ...
- php之cURL惯用
1.php cURL的强大:PHP 支持 Daniel Stenberg 创建的 libcurl 库,能够连接通讯各种服务器.使用各种协议.libcurl 目前支持的协议有 http.https.ft ...
- POJ 3469 Dual Core CPU(最小割模型的建立)
分析: 这类问题的一遍描述,把一些对象分成两组,划分有一些代价,问最小代价.一般性的思路是, 把这两组看成是S点和T点,把划分的代价和割边的容量对应起来求最小割. 把S和可模版tem之间到达关系看作是 ...
- slice 与 substring
Array数组:slice() slice() : 截取 Array 的部分元素,然后返回一个新的Array. var arr = ['a', ' b', 'c', 'd', 'e', 'f', 'g ...
- shiro学习记录(一)
1 权限概述 认证:系统提供的用于识别用户身份的功能,通常登录功能就是认证功能-----让系统知道你是谁?? 授权:系统授予用户可以访问哪些功能的许可(证书)----让系统知道你能做什么?? 2 常见 ...