在我们使用ListView的时候,经常会遇到某一项(Item)需要高亮显示的情况,如下图,有人说当我们点击子项的时候会变亮,但有时候业务逻辑需要让ITEM根据条件自动变亮,下面我来介绍一下我自己的解决办法
1.首先在layout文件夹对应的xml配置文件定义一个listView控件,这里我不做详细介绍了
2.自定义的适配器MyCustomAdapter 用来继承BaseAdapter ,注意最后的setSelectItem方法是关键
public class MyCustomAdapter extends BaseAdapter { private LayoutInflater customInflater; private Listlist; 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 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的设置,这里大家也可以灵活设置,只是需要注意与自己适配器类中的控件对应
5.最重要的一步来了,就是我们如何调用呢,在Activity符合你条件的地方加上
customAdapter.setSelectItem(CURRENT_POSITION); customAdapter.notifyDataSetInvalidated();
我们可以看到setSelectItem是我们第二步自定义适配器里面的方法,用于获得当前的选中的Item项,然后接着调用notifyDataSetInvalidated();就行了,有人可能会发现此处不是用的notifyDataSetChanged(),的确这里我们需要的是对控件改变进行通知,而不是对其中的内容发生改变通知,详细可以了解notifyDataSetInvalidated()与notifyDataSetChanged()的相同不同点。
至次,整个逻辑完成,我们可以灵活控制ListView的某一项高亮显示