CustomAdapter.java:
public class CustomAdapter extends ArrayAdapter<String>{
Context mContext;
String list[];
LayoutInflater mInflater;
public static HashMap<Integer, String> idList=new HashMap<Integer,String>();
public CustomAdapter(Context context, int textViewResourceId,String[] objects) {
super(context, textViewResourceId, objects);
mContext=context;
list=objects;
mInflater=LayoutInflater.from(context);
for(int i=0;i<list.length;i++){
idList.put(i,"false");
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView==null){
convertView=mInflater.inflate(R.layout.list_fruit,null);
holder=new ViewHolder();
holder.mTextView=(TextView)convertView.findViewById(R.id.mTextViewId);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
idList.put(position, "true");
if(idList.get(position)=="true")
holder.mTextView.setBackgroundColor(Color.GRAY);
else
holder.mTextView.setBackgroundColor(Color.WHITE);
return convertView;
}
class ViewHolder{
TextView mTextView;
}
}
list_fruit.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mTextViewId"
android:background="#fff"
android:textColor="#333"
android:padding="5dip"/>
现在,
setListAdapter(new CustomAdapter(this, R.layout.list_fruit,ratios));
final ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setBackgroundColor(Color.LTGRAY);
((TextView) listView.getChildAt(0)).setBackgroundColor(Color.CYAN);
现在,无论单击哪个文本视图,都将变为灰色,其他为白色。