用户点击ListView中的item时,有的时候我们想要选中的item高亮显示,以便提醒用户。原本以为只需要简单设置就能实现,但是并非如此。下面我们来介绍如何让选中的item高亮显示。

方法一:简单实现

此方法的思路就是在ListView的 OnItemClickListener 中,得到当前点击的View ,将其保存在Tag信息中,设置背景色为红色;如果tag信息存在的话,说明用户又单击了其他的View,这个时候将背景色取消即可,代码如下:


//item绑定菜品列表单击监听器
listDishes.setOnItemClickListener(new OnItemClickListener(){



@Override



public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (((ListView)parent).getTag() != null){
((View)((ListView)parent).getTag()).setBackgroundDrawable(null);



}
 
 
((ListView)parent).setTag(view);
 
 
view.setBackgroundColor(Color.RED);
 
 
}
 
 
});


优点:实现简单

缺点:无法外部调用,比如我们想要实现点击按钮,listview自动选中下一个item


方法二: 为ListView实现自定义的适配器

1.我们需要为ListView实现一个自定义的适配器,复写其中的getView方法。如果你不知道怎么做,推荐你先好好阅读一下这里:Android开发之ListView 适配器(Adapter)优化

2.适配器中有一个成员变量,用于接收从外部传进来位置信息

3.在适配器的getView方法中,将用户点击position位置处的View背景设置为屎黄色

参考代码如下:


import java.util.ArrayList;
 
  
import java.util.HashMap;
 
  
import java.util.List;
 
  
import tgb.com.R;
 
  
import android.app.Activity;
 
  
import android.content.Context;
 
  
import android.graphics.Color;
 
  
import android.os.Bundle;
 
  
import android.view.LayoutInflater;
 
  
import android.view.View;
 
  
import android.view.ViewGroup;
 
  
import android.widget.AdapterView;
 
  
import android.widget.BaseAdapter;
 
  
import android.widget.ListView;
 
  
import android.widget.TextView;
 
  
import android.widget.AdapterView.OnItemClickListener;



public class DemoActivity extends Activity {
//显示具体菜品
private ListView listDishes = null;





//具体菜品listview适配器
private MyAdapter myAdapter;





//列表,用来存放类别下的菜品



List<HashMap<String, String>> data;
 
   
  
@Override



protected void onCreate(Bundle savedInstanceState) {



super.onCreate(savedInstanceState);
 
  
setContentView(R.layout.menu);




//取得所有的菜品
data =  this.getDishes();




myAdapter = new MyAdapter(this);





//寻找菜类别控件



listDishes = (ListView)findViewById(R.id.listDishes);



//设置适配器



listDishes.setAdapter(myAdapter);





//绑定菜品列表单击监听器
listDishes.setOnItemClickListener(new OnItemClickListener(){



@Override



public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {



myAdapter.setSelectItem(5);
 
  
myAdapter.notifyDataSetInvalidated();
 
  
}
 
  
});
 
  
}


/**
* 获得菜品名称
* @return
*/
public List<HashMap<String, String>> getDishes() {
//声明一个哈希数组
ArrayList<HashMap<String, String>> list = new  ArrayList<HashMap<String, String>>();
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();
HashMap<String, String> map4 = new HashMap<String, String>();
HashMap<String, String> map5 = new HashMap<String, String>();
HashMap<String, String> map6 = new HashMap<String, String>();
HashMap<String, String> map7 = new HashMap<String, String>();
HashMap<String, String> map8 = new HashMap<String, String>();




//加载菜品信息
map1.put("dish_name", "鱼香肉丝");
map1.put("dish_price", "5元");





map2.put("dish_name", "麻婆豆腐");
map2.put("dish_price", "6元");





map3.put("dish_name", "茅台十五年");
map3.put("dish_price", "2580元");




map4.put("dish_name", "考利干白");
map4.put("dish_price", "168元");




map5.put("dish_name", "汉诺香槟");
map5.put("dish_price", "568元");




map6.put("dish_name", "张裕香槟");
map6.put("dish_price", "168元");




map7.put("dish_name", "杰克丹尼");
map7.put("dish_price", "35元/杯");




map8.put("dish_name", "伏特加");
map8.put("dish_price", "50元/杯");




//添加进列表中



list.add(map1);
 
  
list.add(map2);
 
  
list.add(map3);
 
  
list.add(map4);
 
  
list.add(map5);
 
  
list.add(map6);
 
  
list.add(map7);
 
  
list.add(map8);


return list;



}




/**
* 静态类,用于ListView中存放菜品名称和单价
* @author abing
*
*/
static class ViewHolder {



TextView textDishName;
 
  
TextView textDishPrice;
 
  
}




/**
* ListView适配器,实现有自己布局的Listview
* @author abing
*
*/
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private int  selectItem=-1;
public MyAdapter(Context context) {
this.mInflater = LayoutInflater.from(context);



}



public int getCount() {
// TODO Auto-generated method stub
return data.size();



}



public Object getItem(int arg0) {
// TODO Auto-generated method stub
return data.get(arg0);



}



public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;



}



public View getView(int position, View convertView, ViewGroup parent) {




ViewHolder holder;




if (convertView == null) {
holder = new ViewHolder();
//此处布局文件详见文章最后
convertView = mInflater.inflate(R.layout.list_dishes_detail, null);



holder.textDishName = (TextView) convertView.findViewById(R.id.dish_name);
 
  
holder.textDishPrice = (TextView) convertView.findViewById(R.id.dish_price);
 
   
  
convertView.setTag(holder);



} else {



holder = (ViewHolder) convertView.getTag();
 
  
}



holder.textDishName.setText(data.get(position).get("dish_name"));
holder.textDishPrice.setText(data.get(position).get("dish_price"));
//如果位置相同则设置背景为黄色
if (position == selectItem) {



convertView.setBackgroundColor(Color.YELLOW);
 
  
}



else {



convertView.setBackgroundColor(Color.TRANSPARENT);
 
  
}




return convertView;



}




public  void setSelectItem(int selectItem) {
this.selectItem = selectItem;



}




}



}






R.layout.list_dishes_detail 布局文件如下




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="1dip"
android:paddingBottom="1dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"



>




<TextView



android:id="@+id/dish_name"
android:layout_width="100dip"
android:layout_height="30dip"
android:textSize="11pt"
android:singleLine="true"



/>





<TextView



android:id="@+id/dish_price"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:textSize="11pt"
android:singleLine="true"



/>




</LinearLayout>



优点:功能强大,灵活


缺点:实现较为复杂,新手需要耐心调试

最终效果如图:




ListView android 高亮 安卓实现代码高亮_ListView android 高亮