Android编程心得-ListView的Item高亮显示的办法
在我们使用ListView的时候,经常会遇到某一项(Item)需要高亮显示的情况,如下图,有人说当我们点击子项的时候会变亮,但有时候业务逻辑需要让ITEM根据条件自动变亮,下面我来介绍一下我自己的解决办法
1.首先在layout文件夹对应的xml配置文件定义一个listView控件,这里我不做详细介绍了
<ListView
android:id="@+id/MeterReadingList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@color/gray"
android:dividerHeight="1dp" >
</ListView>
2.自定义的适配器MyCustomAdapter 用来继承BaseAdapter ,注意最后的setSelectItem方法是关键
public class MyCustomAdapter extends BaseAdapter {
private LayoutInflater customInflater;
private List<ReadyTask> list;
private int layoutID; public class ViewHolder {
TextView m_order;
TextView m_MeterID;
TextView m_RFID;
TextView m_Area;
TextView m_clientName;
TextView m_clientAddress; } public MyCustomAdapter(LayoutInflater customInflater, List<ReadyTask> list,
int layoutID) {
this.customInflater =customInflater;
this.list = list;
this.layoutID = layoutID; } @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ReadyTask rTask = (ReadyTask)getItem(position); ViewHolder viewHolder = null;
if (convertView == null) {
convertView = customInflater.inflate(layoutID, null);
viewHolder = new ViewHolder();
viewHolder.m_order=(TextView) convertView.findViewById(R.id.m_order);
viewHolder.m_MeterID=(TextView) convertView.findViewById(R.id.m_MeterID);
viewHolder.m_RFID=(TextView) convertView.findViewById(R.id.m_RFID);
viewHolder.m_Area=(TextView) convertView.findViewById(R.id.m_Area);
viewHolder.m_clientName=(TextView) convertView.findViewById(R.id.m_clientName);
viewHolder.m_clientAddress=(TextView) convertView.findViewById(R.id.m_clientAddress); convertView.setTag(viewHolder); } else {
viewHolder = (ViewHolder) convertView.getTag();
// Log.d("MyCustomAdapter", "旧的convertView,position=" + position);
}
if (list!=null&&list.size()>0){ viewHolder.m_order.setText(String.valueOf(position+1));
viewHolder.m_MeterID.setText(PublicConstant.MeterID+rTask.MeterID);
viewHolder.m_RFID.setText(PublicConstant.RFID+rTask.RFID);
viewHolder.m_Area.setText(PublicConstant.Area+rTask.Area);
viewHolder.m_clientName.setText(PublicConstant.ClientName+rTask.ClientName);
viewHolder.m_clientAddress.setText(PublicConstant.ClientAddress+rTask.ClientAddress); }
if (position == selectItem) {
convertView.setBackgroundColor(Color.CYAN);
}
else {
convertView.setBackgroundColor(Color.TRANSPARENT);
} return convertView;
} public void setSelectItem(int selectItem) {
this.selectItem = selectItem;
}
private int selectItem=-1; }
3.在Activity的OnCreate中,对ListView初始化并找到适配器,
readyTaskList 为我自定义的List,这里大家可以根据自己的逻辑灵活应用
ListViewMeterReadinglist=(ListView) findViewById(R.id.MeterReadingList); customAdapter = new MyCustomAdapter(this, readyTaskList,
R.layout.listview_item); MeterReadinglist.setAdapter(customAdapter);
4.ListView Item的设置,这里大家也可以灵活设置,只是需要注意与自己适配器类中的控件对应
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="2dp"
android:paddingTop="2dp" > <TextView
android:id="@+id/m_order"
android:layout_width="36sp"
android:layout_height="fill_parent"
android:gravity="center"
android:paddingRight="2dp"
android:textSize="18sp"
android:textStyle="bold" /> <View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/gray" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="7"
android:orientation="vertical"
android:paddingBottom="2dp"
android:paddingTop="2dp" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="1dp"
android:orientation="horizontal" > <TextView
android:id="@+id/m_RFID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/m_MeterID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
>
</TextView> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="1dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/m_Area"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" /> <TextView
android:id="@+id/m_clientName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" /> <TextView
android:id="@+id/m_clientAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout> <ImageView
android:id="@+id/RightOrWrong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="TODO" />
</LinearLayout> </LinearLayout>
5.最重要的一步来了,就是我们如何调用呢,在Activity符合你条件的地方加上
customAdapter.setSelectItem(CURRENT_POSITION);
customAdapter.notifyDataSetInvalidated();
我们可以看到setSelectItem是我们第二步自定义适配器里面的方法,用于获得当前的选中的Item项,然后接着调用notifyDataSetInvalidated();就行了,有人可能会发现此处不是用的notifyDataSetChanged(),的确这里我们需要的是对控件改变进行通知,而不是对其中的内容发生改变通知,详细可以了解notifyDataSetInvalidated()与notifyDataSetChanged()的相同不同点。
至次,整个逻辑完成,我们可以灵活控制ListView的某一项高亮显示
Android编程心得-ListView的Item高亮显示的办法的更多相关文章
- [转]Android ListView的Item高亮显示的办法
本文转自:http://www.cnblogs.com/dyllove98/archive/2013/07/31/3228601.html 在我们使用ListView的时候,经常会遇到某一项(Item ...
- Android编程心得-在任意类中获取当前屏幕宽高
进行Android编程时,很多时候都需要获取当前屏幕的宽度与高度,但是当我们需要在别的类中调用屏幕宽高时,直接用原来的方法是不行的,下面我来介绍如何在任意类中调用宽度高度的两种方法. public v ...
- 安卓Android控件ListView获取item中EditText值
可以明确,现在没有直接方法可以获得ListView中每一行EditText的值. 解决方案:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值. 大概算法:重写Ba ...
- Android控件ListView获取item中EditText值
能够明白,如今没有直接方法能够获得ListView中每一行EditText的值. 解决方式:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值. 大概算法:重写Ba ...
- Android 编程下 ListView 的 HeaderView 和 FooterView 不可选择点击
在 ListView 里,HeaderView 和 FooterView 也占一行,与其他的 item 一样,可以点击,有索引,HeaderView 的索引为0.如果要使这两项不可点击,可以使用下面的 ...
- 【转】[Android编程心得] Camera(OpenCV)自动对焦和触摸对焦的实现
参考http://stackoverflow.com/questions/18460647/android-setfocusarea-and-auto-focus http://blog.csdn.n ...
- [Android编程心得] Camera(OpenCV)自动对焦和触摸对焦的实现
写在前面 最近在从零开始写一个移动端的AR系统,坑实在是太多了!!!整个项目使用了OpenCV第三方库,但对于摄像机来说,和原生Camera的方法基本相同. 实现 以OpenCV的JavaCamera ...
- Android编程心得-使用ActionBar+Fragment+ViewPager实现动态切换Menu效果
1.首先上效果图 2.本例实现的效果主要适用于当前页面有多个页签时.进行Fragment切换时,能够利用不同的Menu样式与当前Fragment中的内容进行配合,能够大大添加复用性,看到效果图后,以下 ...
- Android编程心得-JSON使用心得(二)
在Android开发中,我们经常会用到JSON来与网络数据进行交互,下面我来介绍如何对JSON数据进行解析与制造 1.当我们需要对如下JSON串进行制造时: { "download" ...
随机推荐
- work_7
1. 理解C++变量的作用域和生命周期 a) 用少于10行代码演示你对局部变量的生命周期的理解 局部变量分为动态局部变量和静态局部变量,其共同点为作用域均为定义它的函数体或语句块,其不同点为其生命周期 ...
- JavaScript如何判断参数为浮点型
在codewars里,确实可以学到很多很酷的方法,例如这一次的题目是判断数字是否为浮点型.我一开始是想有没有原生的js方法,像isNaN(),isFinite(),在前者Infinity是不属于NaN ...
- UVALive 7456 Least Crucial Node (并查集)
Least Crucial Node 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/C Description http://7 ...
- Spring @PostConstruct and @PreDestroy example
In Spring, you can either implements InitializingBean and DisposableBean interface or specify the in ...
- Hibernate入门(1)-第一个Hibernate程序
Hibernate入门(1)-第一个Hibernate程序 Hibernate是最著名的ORM工具之一,本系列文章主要学习Hibernate的用法,不涉及Hibernate的原理.本文介绍第一个Hib ...
- MSSQL导入数据时,出现“无法截断表 因为表正由Foreign key引用”错误
* 错误 0xc002f210: 准备 SQL 任务: 执行查询“TRUNCATE TABLE [dsc100552_db].[dbo].[ALV_SalesBigClass] ”失败,错误如下:“无 ...
- 数据库MySQL常用命令复习
-- 查看数据库 show databases; -- 创建数据库 create database '数据库名'; -- 删除数据库 drop database '数据库名'; -- 选库 use ' ...
- Educational Codeforces Round 1 A. Tricky Sum 暴力
A. Tricky Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem ...
- 【VMware混合云】应用为王
作者:范军 (Frank Fan) 新浪微博:@frankfan7 微信:frankfan7 你可能会问,VMware混合云不是IaaS么,怎么说起应用了? 如果我们回到原始的问题,搭建Infra ...
- SQL中declare申明变量
在sql语句中加入�变量. declare @local_variable data_type 声明时须要指定变量的类型, 能够使用set和select对变量进行赋值, 在sql语句中就能够使用@lo ...