一、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. java.lang.IllegalArgumentException: Document base F:\personal\projects\annoMVC\web does not exist or is not a readable directory

    tomcat明明在d盘,怎么到F盘了...纳闷啊... 解决办法: 1停掉Tomcat服务2把/conf/Catalina/localhost下的文件删除掉(我在这个文件夹里发现了一个xml,指向了F ...

  2. pandas error记录随笔

    1.sys:1: DtypeWarning: Columns (0,1) have mixed types. Specify dtype option on import or 解决办法:PANDAS ...

  3. 【Shell脚本学习24】Shell输入输出重定向:Shell Here Document,/dev/null文件

    Unix 命令默认从标准输入设备(stdin)获取输入,将结果输出到标准输出设备(stdout)显示.一般情况下,标准输入设备就是键盘,标准输出设备就是终端,即显示器. 输出重定向 命令的输出不仅可以 ...

  4. 编译安装PHP-7.1.8

    安装依赖包: 1.安装yasm cd /usr/local/src tar zxvf yasm-1.3.0.tar.gz cd yasm-1.3.0 ./configure make make ins ...

  5. zabbix-3.4 触发器

    3 触发器 概述 触发器是"评估"由项目采集的数据并表示当前系统状况的逻辑表达式. 当监控项用于采集系统的数据时,始终遵循这些数据是非常不切合实际的,因为这些数据始终在等待一个令人 ...

  6. php使用GD库实现图片水印和缩略图——给图片添加文字水印

    今天呢,就来学习一下在php中使用PD库来实现对图片水印的文字水印方法,不需要PS哦! 首先,准备素材 (1)准备一张图片 (2)准备一张水印(最好是透明的,即背景是白色底) (3)准备一中字体(在电 ...

  7. hdu-1556 Color the ball---树状数组+区间修改单点查询

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1556 题目大意: Problem Description N个气球排成一排,从左到右依次编号为1,2 ...

  8. 【BZOJ2733】[HNOI2012] 永无乡(启发式合并Splay)

    点此看题面 大致题意: 给你一张图,其中每个点有一个权值,有两种操作:在两点之间连一条边,询问一个点所在联通块第\(k\)小的权值. 平衡树 看到第\(k\)小,应该不难想到平衡树. 为了练习\(Sp ...

  9. vue安装环境

    vue安装环境 1. 安装node.js 先在电脑上安装node.js, https://nodejs.org/en/ 可以点击链接安装. 安装成功后,在命令板里检测是否安装成功, node -v 2 ...

  10. python实现二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...