博客园  :: 联系 :: 管理

[转]listview学习

Posted on 2013-05-24 16:57  独孤雁  阅读(210)  评论(0编辑  收藏  举报

转子:http://www.apkbus.com/forum.php?mod=viewthread&tid=94324

1、 ListView单行显示(simple_list_item_1)代码:

  1. public class myListItem1 extends Activity{
  2. @Override
  3. public void onCreate(Bundle savedInstanceState){
  4. super.onCreate(savedInstanceState);

  5. ListView listView = new ListView(this);

  6. List<Map<String, String>> mList = new ArrayList<Map<String, String>>();
  7. for(int i=0; i<10; i++){
  8. Map<String, String> map = new HashMap<String, String>();
  9. map.put("TITLE", "Test Title");
  10. map.put("CONTENT", "Test Content");
  11. mList.add(map);
  12. }

  13. SimpleAdapter adapter = new SimpleAdapter(this,
  14. mList,
  15. android.R.layout.simple_list_item_1,         // List 显示一行item1
  16. new String[]{ "CONTENT" },         // "TITLE",
  17. new int[]{ android.R.id.text1 }
  18. );

  19. listView.setAdapter(adapter);
  20. setContentView(listView);
  21. }
  22. }
复制代码

效果:


2、 ListView双行显示(simple_list_item_2)代码:

  1. public class myListItem2 extends Activity{
  2. @Override
  3. public void onCreate(Bundle savedInstanceState){
  4. super.onCreate(savedInstanceState);

  5. ListView listView = new ListView(this);

  6. List<Map<String, String>> mList = new ArrayList<Map<String, String>>();
  7. for(int i=0; i<10; i++){
  8. Map<String, String> map = new HashMap<String, String>();
  9. map.put("TITLE", "Test Title");
  10. map.put("CONTENT", "Test Content");
  11. mList.add(map);
  12. }

  13. SimpleAdapter adapter = new SimpleAdapter(this,
  14. mList,
  15. android.R.layout.simple_list_item_2,         // List 显示两行item1、item2
  16. new String[]{ "TITLE", "CONTENT" },
  17. new int[]{ android.R.id.text1, android.R.id.text2 }
  18. );

  19. listView.setAdapter(adapter);
  20. setContentView(listView);
  21. }
  22. }
复制代码

效果:


3、 ListView自定义显示代码:

  1. public class MyList extends Activity {
  2. @Override
  3. public void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.mylist);

  6. List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
  7. for (int i = 0; i < 10; i++) {
  8. Map<String, Object> map = new HashMap<String, Object>();
  9. map.put("PIC", R.drawable.pic);         // 加载图片资源
  10. map.put("TITLE", "Test Title");
  11. map.put("CONTENT", "Test Content");
  12. mList.add(map);
  13. }

  14. SimpleAdapter adapter = new SimpleAdapter(this,
  15. mList,
  16. R.layout.listitem,         // 自定义布局格式
  17. new String[] { "PIC", "TITLE", "CONTENT" },
  18. new int[] { R.id.listitem_pic, R.id.listitem_title, R.id.listitem_content }
  19. );

  20. ListView listView = (ListView) findViewById(R.id.list);
  21. listView.setAdapter(adapter);
  22. }
  23. }
复制代码

自定义的 listitem.xml

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="?android:attr/listPreferredItemHeight">

  5. <ImageView android:id="@+id/listitem_pic"
  6. android:layout_width="wrap_content"
  7. android:layout_height="fill_parent"
  8. android:layout_alignParentTop="true"
  9. android:layout_alignParentBottom="true"
  10. android:adjustViewBounds="true"
  11. android:padding="10dip" />

  12. <TextView android:id="@+id/listitem_title"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_toRightOf="@id/listitem_pic"
  16. android:layout_alignParentRight="true"
  17. android:layout_alignParentTop="true"
  18. android:layout_alignWithParentIfMissing="true"
  19. android:gravity="center_vertical"
  20. android:textSize="22sp" />

  21. <TextView android:id="@+id/listitem_content"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:layout_toRightOf="@id/listitem_pic"
  25. android:layout_alignParentBottom="true"
  26. android:layout_alignParentRight="true"
  27. android:layout_below="@id/listitem_title"
  28. android:singleLine="true"
  29. android:ellipsize="marquee"
  30. android:textSize="14sp" />
  31. </RelativeLayout>
复制代码

效果:


4、 GridView自定义显示代码:

  1. public class MyGrid extends Activity {
  2. @Override
  3. public void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.mygrid);

  6. List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
  7. for (int i = 0; i < 10; i++) {
  8. Map<String, Object> map = new HashMap<String, Object>();
  9. map.put("PIC", R.drawable.pic);         // 加载图片资源
  10. map.put("TITLE", "Test Title");
  11. mList.add(map);
  12. }
  13. SimpleAdapter adapter = new SimpleAdapter(this,
  14. mList, R.layout.griditem,         // 自定义布局格式
  15. new String[] { "PIC", "TITLE" },
  16. new int[] { R.id.griditem_pic, R.id.griditem_title, }
  17. );

  18. GridView gridView = (GridView) findViewById(R.id.grid);
  19. gridView.setAdapter(adapter);
  20. }
  21. }
复制代码

自定义的 gridview.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_height="fill_parent"
  4. android:layout_width="fill_parent"
  5. android:orientation="vertical">

  6. <ImageView android:id="@+id/griditem_pic"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_gravity="center_horizontal">
  10. </ImageView>

  11. <TextView android:id="@+id/griditem_title"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_gravity="center_horizontal">
  15. </TextView>
  16. </LinearLayout>
复制代码

效果:



mySimpleAdapter.7z.zip (43.71 KB, 下载次数: 207)