簡約而不簡單——Android SimpleAdapter

來源:互聯網
上載者:User

列表(ListView)、表格(GridView),這在手機應用上面肯定是少不了的,怎樣實現比較複雜一點的介面呢,先看一下我的。

這樣布局的情況是最基本的,也是最常用的,網上關於這樣的布局有多種版本的實現方法,但是有很多需要自己實現Adapter,那樣子是比較複雜而且沒有必要的,因為我們有簡約而不簡單的SimpleAdapter。

1. ListView

SimpleAdapter的核心代碼:

for (int i = 0; i < 10; i++) {Map<String, Object> map = new HashMap<String, Object>();map.put("PIC", R.drawable.pic);map.put("TITLE", "Test Title");map.put("CONTENT", "Test Content");contents.add(map);}SimpleAdapter adapter = new SimpleAdapter(this,(List<Map<String, Object>>) contents, R.layout.listitem,new String[] { "PIC", "TITLE", "CONTENT" }, new int[] {R.id.listitem_pic, R.id.listitem_title,R.id.listitem_content });listView.setAdapter(adapter);

 

listitem的Layout:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"><ImageView android:id="@+id/listitem_pic"android:layout_width="wrap_content" android:layout_height="fill_parent"android:layout_alignParentTop="true" android:layout_alignParentBottom="true"android:src="@drawable/pic" android:adjustViewBounds="true"android:padding="2dip" /><TextView android:id="@+id/listitem_title"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_toRightOf="@+id/listitem_pic"android:layout_alignParentRight="true" android:layout_alignParentTop="true"android:layout_above="@+id/listitem_content"android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical"android:text="@+id/listitem_title" android:textSize="22px" /><TextView android:id="@+id/listitem_content"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_toRightOf="@+id/listitem_pic"android:layout_alignParentBottom="true"android:layout_alignParentRight="true" android:singleLine="true"android:ellipsize="marquee" android:text="@+id/item_content"android:textSize="14px" /></RelativeLayout>

 

 

2. GridView

SimpleAdapter的核心代碼:

for (int i = 0; i < 10; i++) {Map<String, Object> map = new HashMap<String, Object>();map.put("PIC", R.drawable.pic);map.put("TITLE", "Test Title");contents.add(map);}SimpleAdapter adapter = new SimpleAdapter(this,(List<Map<String, Object>>) contents, R.layout.griditem,new String[] { "PIC", "TITLE" }, new int[] { R.id.griditem_pic,R.id.griditem_title, });gridView.setAdapter(adapter);

griditem的Layout:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="fill_parent" android:layout_width="fill_parent"android:orientation="vertical"><ImageView android:id="@+id/griditem_pic"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_gravity="center_horizontal"></ImageView><TextView android:id="@+id/griditem_title"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_gravity="center_horizontal" android:text="test"></TextView></LinearLayout>

 

最後附上代碼:http://files.cnblogs.com/game-over/test.zip

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.