在上一个教程中,我们使用SimpleAdapter实现自定义ListView,但是有一点不方便的是,如果要对每一个ListView的item选项进行不同的样式设置,就很麻烦,因为SimpleAdapter使用的是统一的风格样式。如果要实现不同item使用不同的风格样式,那么就要通过集成BaseAdapter来实现。BaseAdapter类最重要的方法是重写getView方法。
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Step1 String[] contentArray = new String[]{"内容1","内容2","内容3","内容4","内容5"}; ListView lv = (ListView)findViewById(R.id.lv); //Step2 ArrayList<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>(); for(int i=0; i<contentArray.length; i++){ HashMap<String,Object> map = new HashMap<String,Object>(); map.put("startTimeText", "22:00"); map.put("endTimeText", "23:00"); map.put("separator", R.drawable.separator1); map.put("content", contentArray[i]); data.add(map); } //Step3 MyAdapter adapter = new MyAdapter(MainActivity.this,data); //Step4 lv.setAdapter(adapter); } class MyAdapter extends BaseAdapter{ private Context mContext; private List<HashMap<String,Object>>dataList; private int[] imageArray = {R.drawable.separator1, R.drawable.separator2, R.drawable.separator3, R.drawable.separator4, R.drawable.separator5}; public MyAdapter(Context context, List<HashMap<String,Object>>dataList) { this.mContext = context; this.dataList = dataList; } @Override public int getCount() { return dataList.size(); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //要想对不同的item选项进行设置,就要重写getView方法来实现 ViewHolder holder = null; if(convertView == null){ holder = new ViewHolder(); convertView = LayoutInflater.from(mContext).inflate( R.layout.list_item, null); holder.separatorImage = (ImageView)convertView.findViewById(R.id.separator); holder.content = (TextView)convertView.findViewById(R.id.content); holder.startTimeText = (TextView)convertView.findViewById(R.id.startTimeText); holder.endTimeText = (TextView)convertView.findViewById(R.id.endTimeText); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } //给listview下不同item选项设置不同的图片样式 holder.separatorImage.setImageResource(imageArray[position]); holder.content.setText((String)getItem(position).get("content")); holder.startTimeText.setText((String)getItem(position).get("startTimeText")); holder.endTimeText.setText((String)getItem(position).get("endTimeText")); return convertView; } @Override public HashMap<String, Object> getItem(int position) { return dataList.get(position); } final class ViewHolder{ ImageView separatorImage; TextView startTimeText; TextView endTimeText; TextView content; } } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>(2)ListView列表单元的布局源码list_item.xml:
<?xml version="1.0" encoding="utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:gravity="center_vertical" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:gravity="center_vertical" android:orientation="vertical"> <TextView android:id="@+id/startTimeText" android:layout_height="wrap_content" android:layout_width="match_parent"/> <TextView android:id="@+id/endTimeText" android:layout_height="wrap_content" android:layout_width="match_parent"/> </LinearLayout> <ImageView android:id="@+id/separator" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/content" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="12"/> </LinearLayout>
res/drawable-mdpi/下的文件:
separator1.png separator2.png separator3.png separator4.png separator5.png