今天打算修改一个drawerLayout的源码时准备修改一个ListView,原版的ListView中的Item均为TextView,然而我想做成ImageView+TextView的。而之前由于突击学习对ListView不甚了解,在修改Item的xml文件后报错。
之后查阅了一下资料,有关ListView和Adapter的使用可以参考此文
http://l62s.iteye.com/blog/1675509
针对此文中讲述的知识,准备编写一个测试项目,暂且mark一记,稍后贴上我自己的源码。
——————————update 一下源代码————————————————————
首先需要一个listItem的xml文件
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/imgButton"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="@drawable/quit"
android:scaleType="fitXY"
/>
<TextView
android:id="@+id/text1"
android:layout_toRightOf="@id/imgButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
</RelativeLayout>
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
ArrayList<HashMap<String,Object>> listItem = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = null;
map = new HashMap<String,Object>();
map.put("ItemImage", R.drawable.ic_launcher);
map.put("ItemText", "导航");
listItem.add(map);
map = new HashMap<String,Object>();
map.put("ItemImage", R.drawable.ic_launcher);
map.put("ItemText", "账户");
listItem.add(map);
SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,R.layout.drawer_list_item,new String[] {"ItemImage","ItemText"}, new int[] {R.id.image,R.id.text1});
mDrawerList.setAdapter(mSimpleAdapter);