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