在我们使用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高亮显示的办法的更多相关文章

  1. [转]Android ListView的Item高亮显示的办法

    本文转自:http://www.cnblogs.com/dyllove98/archive/2013/07/31/3228601.html 在我们使用ListView的时候,经常会遇到某一项(Item ...

  2. Android编程心得-在任意类中获取当前屏幕宽高

    进行Android编程时,很多时候都需要获取当前屏幕的宽度与高度,但是当我们需要在别的类中调用屏幕宽高时,直接用原来的方法是不行的,下面我来介绍如何在任意类中调用宽度高度的两种方法. public v ...

  3. 安卓Android控件ListView获取item中EditText值

    可以明确,现在没有直接方法可以获得ListView中每一行EditText的值. 解决方案:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值. 大概算法:重写Ba ...

  4. Android控件ListView获取item中EditText值

    能够明白,如今没有直接方法能够获得ListView中每一行EditText的值. 解决方式:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值. 大概算法:重写Ba ...

  5. Android 编程下 ListView 的 HeaderView 和 FooterView 不可选择点击

    在 ListView 里,HeaderView 和 FooterView 也占一行,与其他的 item 一样,可以点击,有索引,HeaderView 的索引为0.如果要使这两项不可点击,可以使用下面的 ...

  6. 【转】[Android编程心得] Camera(OpenCV)自动对焦和触摸对焦的实现

    参考http://stackoverflow.com/questions/18460647/android-setfocusarea-and-auto-focus http://blog.csdn.n ...

  7. [Android编程心得] Camera(OpenCV)自动对焦和触摸对焦的实现

    写在前面 最近在从零开始写一个移动端的AR系统,坑实在是太多了!!!整个项目使用了OpenCV第三方库,但对于摄像机来说,和原生Camera的方法基本相同. 实现 以OpenCV的JavaCamera ...

  8. Android编程心得-使用ActionBar+Fragment+ViewPager实现动态切换Menu效果

    1.首先上效果图 2.本例实现的效果主要适用于当前页面有多个页签时.进行Fragment切换时,能够利用不同的Menu样式与当前Fragment中的内容进行配合,能够大大添加复用性,看到效果图后,以下 ...

  9. Android编程心得-JSON使用心得(二)

    在Android开发中,我们经常会用到JSON来与网络数据进行交互,下面我来介绍如何对JSON数据进行解析与制造 1.当我们需要对如下JSON串进行制造时: { "download" ...

随机推荐

  1. cmake编译win下64位obs

    obs是一款开源编码推流工具,简单易用,非常流行.一次项目中,发现本台式机I3处理器下32位obs推流CPU使用率100%.而使用的第三方设备在64位下,性能较好.所以需要编译64位obs并且编译相应 ...

  2. Java设计模式系列之动态代理模式(转载)

    代理设计模式 定义:为其他对象提供一种代理以控制对这个对象的访问. 动态代理使用 java动态代理机制以巧妙的方式实现了代理模式的设计理念. 代理模式示例代码 public interface Sub ...

  3. jQuery基础学习7——层次选择器find()方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. poj3687

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9028   Accepted: 2444 De ...

  5. 十,选择cfee编辑器并学会调试程序。以及结束语。

    为什么推荐用cfree呢?因为我认为这个编辑器界面友好,用起来方便. 你也许会问,调试程序是什么? 那么下面思考几个问题:对于前面讲的分支结构和循环结构有点不懂怎么办?如果写的程序语法没有错误但是运算 ...

  6. struts2的action的知识点和利用action向页面注入值的操作

    1.      Action的顺序,会先搜索指定名字下的包的action,如果找不到会去搜索默认路径下的包下的action. 2.      如果没有给action设置值,那么action会有一些默认 ...

  7. SqlServer更新视图存储过程函数脚本

    --视图.存储过程.函数名称 DECLARE @NAME NVARCHAR(255); --局部游标 DECLARE @CUR CURSOR --自动修改未上状态为旷课 SET @CUR=CURSOR ...

  8. 中国软件开发project师之痛

    在最近的一次会议上,有高层谈到之前在中国觉得自己做得非常牛,但与美国同行接触后却发现与人家存在非常大的差距,这一点我在外企工作时也有过相同的体会.真正与外国同行接触后才会知道什么是差距,在这篇文章中我 ...

  9. 解决Android上的QPython不能import urllib的问题

    试用了一下QPython,感觉很强大,Kivy也包含进去了,下载一些第三方库也很方便,相对于SL4A来说确实先进了很多. 但是很快发现不能import urllib,提示大概是这样的内容: No mo ...

  10. C#操作Word (1)Word对象模型

    Word对象模型  (.Net Perspective) 本文主要针对在Visual Studio中使用C# 开发关于Word的应用程序 来源:Understandingthe Word Object ...