android中為我們提供了3種adapter來為各種各樣的list來適配數據,其ArrayAdapter 對只有純字符列表的list適應,如果遇到list的每個item都有添加幾項的情況,如下圖,有頭像,名字,作品,人氣等,那么ArrayAdapter就不是適用了,就可以使用SimpleAdapter 了,而baseAdapter 用起來對初學者有點難,建議使用simpleAdapter。首先來看整體的布局文件:
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="@string/tab_author" />
<ListView
android:id="@+id/author_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
</ListView>
</LinearLayout>
~~~

其中定義了一個列表的標題,下面定義了一個列表,列表中的scroolbars=“none”是隱藏滾動條。接下來就要為每個item設置布局了,分別有頭像,作者,作品,人氣。
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="7dp"
android:background="#FFdddddd" >
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/list_item_author_touxiang"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="10dp"
android:src="@drawable/icon_stub" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<TextView
android:id="@+id/list_item_author"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="作者"
android:textSize="18sp"/>
<TextView
android:id="@+id/list_item_author_zuopin"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:text="作品:"
android:textColor="#FF777777" />
<TextView
android:id="@+id/list_item_author_renqi"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:text="人氣:"
android:textColor="#FF777777" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
~~~

然后我們就可以在java代碼中編寫程序,來實現一個漂亮的列表;
~~~
public class AuthorListFragment extends Fragment {
private ListView listView;
private View authorList;
private String[] authors = {"周杰倫","王力宏","郭敬明",
"周杰倫","王力宏","郭敬明",
"周杰倫","王力宏","郭敬明"};
private String[] zuopin = {"《搞笑》《東風破》","《落葉歸根》","《圍城》《小時代》",
"《搞笑》《東風破》","《落葉歸根》","《圍城》《小時代》",
"《搞笑》《東風破》","《落葉歸根》","《圍城》《小時代》"};
private String[] renqi = {"9999","12345","98687","9999","12345","98687","9999","12345","98687"};
private int[] touxiang = {R.drawable.zhoujielun,R.drawable.wanglihong,R.drawable.guojingm,
R.drawable.zhoujielun,R.drawable.wanglihong,R.drawable.guojingm,
R.drawable.zhoujielun,R.drawable.wanglihong,R.drawable.guojingm};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
authorList = inflater.inflate(R.layout.author_list, container,false);
viewHolder();
List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
for(int i = 0; i < authors.length; i++){
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("touxiang", touxiang[i]);
map.put("name", authors[i]);
map.put("zuopin", "作品:"+zuopin[i]);
map.put("renqi", "人氣:"+renqi[i]);
data.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(getActivity(), data, R.layout.author_list_item, new String[]{"touxiang","name","zuopin","renqi"},
new int[]{R.id.list_item_author_touxiang,
R.id.list_item_author,
R.id.list_item_author_zuopin,
R.id.list_item_author_renqi});
listView.setAdapter(adapter);
return authorList;
}
private void viewHolder() {
listView = (ListView) authorList.findViewById(R.id.author_list);
}
}
~~~
我這是在一個fragment里面實現的,大家可以在Activity里面試試,都是能 行的。我在真機上面運行的,我截個圖給大家看看:

? 到此為止,simpleadapter就能實現很漂亮的列表界面。 祝大家開開興興,每天進步一點!88
- 前言
- viewpager
- 實現橫向listview(HorizontalListview)
- SimpleAdapter的使用
- 自定義android圓形ImageView
- 如何解決listView或scrollView+viewpager手勢沖突的問題
- EditText怎樣設置成下劃線
- Android中Display及DisplayMetrics理解
- android開發小經驗總結
- listView動態影藏顯現列表項中的多項部分
- Android開發中怎樣使用cookieManager來管理cookie
- 學習Android從0開始之背景篇-Android系統介紹
- 學習Android從0開始之開發工具篇-Android studio詳解
- 學習Android從0開始之基礎篇(1)-Android的四大基本組件
- 學習Android從0開始之基礎篇(2)-AndroidMainfest.xml文件詳解
- 學習Android從0開始之基礎篇(3)-視圖組件之布局管理器
- 學習Android從0開始之基礎篇(4)-TextView與EditText