listView中继承BaseAdapter并且扩展LinearLayout
定义一个对象:
public class Weather { public static final int NA = -1; public static final int SUNNY = 0; public static final int OVERCAST = 1; public static final int RAIN = 2; public String city = null; public int temperature = 0; public int sky = NA; public Weather( String city, int temperature, int sky ) { this.city = city; this.temperature = temperature; this.sky = sky; } public String getCity() {return city; } public int getTemperature() {return temperature; } public int getSkyResource() {switch( sky ) { case SUNNY:return R.drawable.weather_sunny; case OVERCAST:return R.drawable.weather_overcast; case RAIN:return R.drawable.weather_rain;}return R.drawable.unknown; }}?
主activity
public class CustomAdapterActivity extends ListActivity{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList<Weather> weatherList = new ArrayList<Weather>(); Weather w = new Weather( "London", 17, Weather.OVERCAST ); weatherList.add( w ); w = new Weather( "Paris", 22, Weather.OVERCAST ); weatherList.add( w ); w = new Weather( "Athens", 29, Weather.SUNNY ); weatherList.add( w ); w = new Weather( "Stockholm", 12, Weather.RAIN ); weatherList.add( w ); WeatherAdapter weatherAdapter = new WeatherAdapter( this,weatherList ); setListAdapter( weatherAdapter ); }}?
最关键的就是 WeatherAdapter
class WeatherAdapterView extends LinearLayout { public static final String LOG_TAG = "WeatherAdapterView"; public WeatherAdapterView(Context context, Weather weather ) { super( context ); this.setOrientation(HORIZONTAL); LinearLayout.LayoutParams cityParams = new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT); cityParams.setMargins(1, 1, 1, 1); TextView cityControl = new TextView( context ); cityControl.setTextAppearance( context, R.style.SpecialText );cityControl.setText( weather.getCity() ); addView( cityControl, cityParams); LinearLayout.LayoutParams temperatureParams = new LinearLayout.LayoutParams(20, LayoutParams.WRAP_CONTENT); temperatureParams.setMargins(1, 1, 1, 1); TextView temperatureControl = new TextView(context); temperatureControl.setText( Integer.toString( weather.temperature ) ); addView( temperatureControl, temperatureParams); LinearLayout.LayoutParams skyParams = new LinearLayout.LayoutParams(25, LayoutParams.WRAP_CONTENT);ImageView skyControl = new ImageView( context ); Log.d( LOG_TAG, weather.getCity()+" -> "+weather.sky );skyControl.setImageResource( weather.getSkyResource() );addView( skyControl, skyParams ); }}public class WeatherAdapter extends BaseAdapter { private Context context; private List<Weather> weatherList; public WeatherAdapter(Context context, List<Weather> weatherList ) { this.context = context; this.weatherList = weatherList; } public int getCount() { return weatherList.size(); } public Object getItem(int position) { return weatherList.get(position); } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { Weather weather = weatherList.get(position); return new WeatherAdapterView(this.context, weather ); }}?
通过定义一个对象 在这个对象中包含要显示的东西,这样就不用使用simpleAdapater了,不同的方法自己选择吧。
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyTheme" parent="android:Theme.Light"> <item name="android:listViewStyle">@style/MyListView</item> </style> <style name="SpecialText" parent="@android:style/TextAppearance"> <item name="android:textSize">18sp</item> <item name="android:textStyle">bold</item> <item name="android:textColor">#008</item> </style> <style name="MyListView" parent="@android:style/Widget.ListView"> <item name="android:background">@color/opaque_red</item> <item name="android:listSelector">@drawable/z_selector_background</item> </style></resources>
?
别忘了在主xml中加入上面的主题。
源文件上传了 这里面也使用到了selector