listView中扩展ArrayAdapter,以及view的使用

public class SelectorDemo extends ListActivity {
	private static ColorStateList allWhite=ColorStateList.valueOf(0xFFFF0000);
	private static String[] items={"lorem", "ipsum", "dolor","purus"};
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
		setListAdapter(new SelectorAdapter(this));
		getListView().setOnItemSelectedListener(listener);
	}
	
	class SelectorAdapter extends ArrayAdapter {
		SelectorAdapter(Context ctxt) {
			super(ctxt,R.layout.row, items);
			
		}
		
		@Override
		public View getView(int position, View convertView,ViewGroup parent) {
			SelectorWrapper wrapper=null;
			
			if (convertView==null) {
				convertView=getLayoutInflater().inflate(R.layout.row,parent, false);
				wrapper=new SelectorWrapper(convertView);
				wrapper.getLabel().setTextColor(allWhite);
				convertView.setTag(wrapper);
			}
			else {
				wrapper=(SelectorWrapper)convertView.getTag();
			}
			
			wrapper.getLabel().setText(items[position]);
			
			return(convertView);
		}
	}
	
	class SelectorWrapper {
		View row=null;
		TextView label=null;
		View bar=null;
		
		SelectorWrapper(View row) {
			this.row=row;
		}
		
		TextView getLabel() {
			if (label==null) {
				label=(TextView)row.findViewById(R.id.label);
			}
			
			return(label);
		}
		
		View getBar() {
			if (bar==null) {
				bar=row.findViewById(R.id.bar);
			}
			
			return(bar);
		}
	}
	
	AdapterView.OnItemSelectedListener listener=new AdapterView.OnItemSelectedListener() {
		View lastRow=null;
		
		public void onItemSelected(AdapterView<?> parent, View view, int position,
													 long id) {
			Log.i("view","parent:"+parent+"view:"+view+position);
			
			if (lastRow!=null) {
				SelectorWrapper wrapper=(SelectorWrapper)lastRow.getTag();

				wrapper.getBar().setVisibility(View.INVISIBLE);
			}
			
				
			SelectorWrapper wrapper=(SelectorWrapper)view.getTag();
			
			wrapper.getBar().setVisibility(View.VISIBLE);
			lastRow=view;
		}
		
		public void onNothingSelected(AdapterView<?> parent) {
			//Log.i("onNothingSelected","parent:"+parent);
			if (lastRow!=null) {
				SelectorWrapper wrapper=(SelectorWrapper)lastRow.getTag();

				wrapper.getBar().setVisibility(View.INVISIBLE);
				lastRow=null;
			}
		
		}
	};
}

 main

<?xml version="1.0" encoding="utf-8"?>
<ListView
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@android:id/list"
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent"
	android:listSelector="#00000000"
/>

 

最关键的就是下面的了

<?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" >
	<View
		android:id="@+id/bar"
		android:background="#FFFFFF00"
		android:layout_width="5px" 
		android:layout_height="fill_parent"
		android:visibility="invisible"
	/>
	<TextView
		android:id="@+id/label"
		android:layout_width="fill_parent" 
		android:layout_height="fill_parent"
		android:textSize="10pt"
		android:paddingTop="2px"
		android:paddingBottom="2px"
		android:paddingLeft="5px"
	/>
</LinearLayout>

 

这个view就是一条横线了而是一条竖线

你可能感兴趣的:(android,xml)