一、listView被选中后保持高亮

70down voteaccepted

To hold the color of listview item when you press it, include the following line in your listview layout:

android:background="@drawable/bg_key"

Then define bg_key.xml in drawable folder like this:

 
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
</selector>

Finally, include this in your listview onClickListener:

listView.setOnItemClickListener(new OnItemClickListener() { 
 
@Override 
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
... 

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"?>
<resources>
<color name="pressed_color">#4d90fe</color>
<color name="default_color">#ffffff</color>
</resources>

二、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如何选中时保持高亮?的更多相关文章

  1. 改变listview中item选中时文字的颜色

    摘要 当listview的某个item选中时,默认有个选中的高亮显示,如果你要自定义选中时的高亮显示效果,可以在listview中设置属性 android:listSelector="@dr ...

  2. iOS UITableView 移除单元格选中时的高亮状态

    在处理UITableView表格时,我们希望用户能够和触摸单元格式进行交互,但是希望用户在完成交互之后,这些单元格的选中状态能够消失,.Cocoa Touch 提供了两种方法来防止单元格背持久选中. ...

  3. listview更改选中时item背景色(转)

    默认情况下使用ListView背景色是黑色,选中item的高亮颜色是菊黄色,很多时候不得不自己定义背景色或者背景图 android:cacheColorHint="@android:colo ...

  4. vs 2012 设置选中的引用高亮 颜色

    一个简单的小技巧,不用插件也可以做到,vs 2012 设置选中的引用高亮 颜色 .vs 默认的单击引用的变量时,显示的颜色是灰色,基本看不出来,所有特意抽空捣鼓了下 还真让我给找出来 工具==> ...

  5. 设置 TabBarItem 选中时的图片及文字颜色

    TabBarController 是在 ios 开发过程中使用较为频繁的一个 Controller,但是在使用过程中经常会遇到一些问题,例如本文所要解决的,如何修改 TabBar 选中时文字及图片的颜 ...

  6. 转 Android RadioButton设置选中时文字和背景颜色同时改变

    主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿. 搜了一下,效果和这个文章一致.转了. 原文地址:http:// ...

  7. IOS - UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte

    1.系统默认的颜色设置 [cpp] view plaincopy //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 ...

  8. UITableViewCell的选中时的颜色设置

    转自:http://hi.baidu.com/zhu410289616/item/0de0262910886011097508c2 1.系统默认的颜色设置 //无色 cell.selectionSty ...

  9. IOS中设置cell的背景view和选中时的背景view 、设置cell最右边的指示器(比如箭头\文本标签)

    一.Cell的设置 1.设置cell的背景view和选中时的背景view UIImageView *bg = [[UIImageView alloc] init]; bg.image = [UIIma ...

随机推荐

  1. 菜鸟 学注册机编写之 “sha1”

    1. 首先运行程序随便输入用户与注册码如下图所示: 2.将程序载入OD, 下MessageBoxA函数断点, F9运行程序, 程序运行后随便输入用户名与注册码,点"OK"后断下,F ...

  2. iOS编程规范(整理)

    一.文档结构管理 1.建立Libraries文件夹,所有第三方库放入其中. 2.建立Utilities文件夹,自已封装的类放入其中. 3.建立Constants.h头文件,所有的常量定义于其中.Con ...

  3. 青松云安全-WAF-1.0.655 (ubuntu 14.04 server)

    平台: Ubuntu 类型: 虚拟机镜像 软件包: web application firewall basic software security ubuntu waf 服务优惠价: 按服务商许可协 ...

  4. 第2章 核心C#

    1. 变量 1.1 变量需要遵循的规则: 变量必须初始化 初始化器不能为空 初始化器必须放在表达式中 不能把初始化器设置为一个对象,除非在初始化器中创建了一个新对象 1.2 变量的作用域 只要类在某个 ...

  5. VMware虚拟机配置文件(.vmx)损坏修复

    我的虚拟机为VM14    装的ubuntu14.04server版 遇到ubuntu打不开,上网查阅了博客写的解决办法,尝试并解决了,以下分享个人心得: 首先进入虚拟机中系统安装的位置 查看日志文件 ...

  6. echarts 相关属性介绍

    title: {//图表标题     x: 'left', //组件离容器左侧的距离,left的值可以是像20,这样的具体像素值, 可以是像 '20%' 这样相对于容器高宽的百分比,也可以是 'lef ...

  7. #linux 下Sublime的安装

    1.Download http://www.sublimetext.com/2 Installtion use tar  解压压缩包,这里我将包改了个名字,这样就不用写空格的转义字符了,改成Subli ...

  8. C# 理解FileInfo类的Open()方法

    我们在对文件进行读写操作之前必须打开文件,FileInfo类为我们提供了一个Open()方法,该方法包含了两个枚举类型值的参数,一个为FileMode枚举类型值,另一个为FileAccess枚举类型值 ...

  9. css代码

    #footr { background: #3e434a } #header #blogTitle { background: url("http://images.cnblogs.com/ ...

  10. Java 性能优化的五大技巧

    要对你的 Java 代码进行优化,需要理解 Java 不同要素之间的相互作用,以及它是如何与其运行时的操作系统进行交互的.使用下面这五个技巧和资源,开始学习如何分析和优化你的代码吧. 在我们开始之前, ...