在我们使用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的某一项高亮显示