安卓UI组件之ListView详解

ListView是很常见的一个UI组件,在许多App中都很常用,其意思就是可滚动的列表,使用ListView必须使用Adapter(适配器),常用的适配器友谊ArrayAdapter,SimpleAdapter,BaseAdapter.

ListView一些特殊的属性是:

android:divider=”@drawable/red” //每个列表项之间的分割线
android:entries=”@drawable/books” //列表项显示的数据

 

布局代码示例:

<ListView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:divider=”@drawable/red”
android:entries=”@drawable/books”>
</LIstView>

 

第一我们使用SimpleAdapter来写一个ListView:

[java][/java] 

  1. public class SimpleAdapter extends Activity{
  2. private String[] names =new String[]{“nick”,”lebron”,”kobe”,”kevin”};
  3. private int[] imgsIds[] =new Int[] {R.drawable.1,R.drawable.2,R.drawable.3,R.drawable.4};
  4. @Override
  5. public void onCreate(Bundle savedInsatnceState)
  6. {
  7. super.onCreate(savedInsatnceState);
  8. setTitle(“simpleadapter”);
  9. setContentView(R.layout.main);
  10. List<Map><String,Object> list=new ArrayList<Map><String,Object>();//创建动态数组ArrayList
  11. for(int i=0;i<names.length;i++){
  12. Map<String,Object> map=new HashMap=<String,Object>;
  13. map.put(“name”,names[i]);
  14. map.put(“headerImg”,imgsIds[i]);
  15. list.add(map);
  16. }
  17. SimpleAdapter adapter=new SimpleAdaper(this,list,R.layout.main,new String[]{“name”,”headerImg”},new int[]{R.id.name,R.id.headerImg});
  18. ListView list=(ListView)findViewById(R.id.mylist);
  19. list.setAdaper(adapter);
  20. }
  21. }

*注释*
SimpleAdapter是另一种常见的adapter类型,其有五个参数
1:Activity.this
2:传入的数据
3:数据传入的界面
4:数据中Map类型数据的String 类的key
5:穿入界面的组件的Id
**

 

第二种我们使用ArrayAdapter:代码不详解,只是重写Adapter:

 

[java][/java] 

  1. String[] str={“china”,”america”,”japan”};
  2. List list=new ArrayList<String>();
  3. for(int i=0;i<str.length;i++){
  4. list.add(str[i]);
  5. }
  6. ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.simple_list_item2,list);

*注释*

 

第三个属性指定要显示每一个ListItem的风格:

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.simple_list_item1,list);:普通文本框
ArrayAdapter<String> adapter=new ArrayAdapetr<String>(this,R.layout.simple_list_multiple_choice);:多选框
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.simple_list_single_choice);:单选框

**

 

另外,ListView还有一个拓展组件ExpandableListView,就是可拓展的列表,但是他的适配器必须使用ExpandableAdapter:

[java][/java] 

  1. public class ExpandableListViewTest extends Activity
  2. {
  3.     @Override
  4.     public void onCreate(Bundle savedInstanceState)
  5.     {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.main);
  8.         //创建一个BaseExpandableListAdapter对象
  9.         ExpandableListAdapter adapter = new BaseExpandableListAdapter()
  10.         {
  11.             int[] logos = new int[]
  12.             {
  13.                 R.drawable.p,
  14.                 R.drawable.z,
  15.                 R.drawable.t
  16.             };
  17.             private String[] armTypes = new String[]
  18.                 { “神族兵种”, “虫族兵种”, “人族兵种”};
  19.             private String[][] arms = new String[][]
  20.             {
  21.                 { “狂战士”, “龙骑士”, “黑暗圣堂”, “电兵” },
  22.                 { “小狗”, “刺蛇”, “飞龙”, “自爆飞机” },
  23.                 { “机枪兵”, “护士MM” , “幽灵” }
  24.             };
  25.             //获取指定组位置、指定子列表项处的子列表项数据
  26.             @Override
  27.             public Object getChild(int groupPosition, int childPosition)
  28.             {
  29.                 return arms[groupPosition][childPosition];
  30.             }
  31.             @Override
  32.             public long getChildId(int groupPosition, int childPosition)
  33.             {
  34.                 return childPosition;
  35.             }
  36.             @Override
  37.             public int getChildrenCount(int groupPosition)
  38.             {
  39.                 return arms[groupPosition].length;
  40.             }
  41.             private TextView getTextView()
  42.             {
  43.                 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
  44.                         ViewGroup.LayoutParams.FILL_PARENT, 64);
  45.                 TextView textView = new TextView(ExpandableListViewTest.this);
  46.                 textView.setLayoutParams(lp);
  47.                 textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
  48.                 textView.setPadding(36, 0, 0, 0);
  49.                 textView.setTextSize(20);
  50.                 return textView;
  51.             }
  52.             //该方法决定每个子选项的外观
  53.             @Override
  54.             public View getChildView(int groupPosition, int childPosition,
  55.                     boolean isLastChild, View convertView, ViewGroup parent)
  56.             {
  57.                 TextView textView = getTextView();
  58.                 textView.setText(getChild(groupPosition, childPosition).toString());
  59.                 return textView;
  60.             }
  61.             //获取指定组位置处的组数据
  62.             @Override
  63.             public Object getGroup(int groupPosition)
  64.             {
  65.                 return armTypes[groupPosition];
  66.             }
  67.             @Override
  68.             public int getGroupCount()
  69.             {
  70.                 return armTypes.length;
  71.             }
  72.             @Override
  73.             public long getGroupId(int groupPosition)
  74.             {
  75.                 return groupPosition;
  76.             }
  77.             //该方法决定每个组选项的外观
  78.             @Override
  79.             public View getGroupView(int groupPosition, boolean isExpanded,
  80.                     View convertView, ViewGroup parent)
  81.             {
  82.                 LinearLayout ll = new LinearLayout(ExpandableListViewTest.this);
  83.                 ll.setOrientation(0);
  84.                 ImageView logo = new ImageView(ExpandableListViewTest.this);
  85.                 logo.setImageResource(logos[groupPosition]);
  86.                 ll.addView(logo);
  87.                 TextView textView = getTextView();
  88.                 textView.setText(getGroup(groupPosition).toString());
  89.                 ll.addView(textView);
  90.                 return ll;
  91.             }
  92.             @Override
  93.             public boolean isChildSelectable(int groupPosition, int childPosition)
  94.             {
  95.                 return true;
  96.             }
  97.             @Override
  98.             public boolean hasStableIds()
  99.             {
  100.                 return true;
  101.             }
  102.         };
  103.         ExpandableListView expandListView = (ExpandableListView)
  104.             findViewById(R.id.list);
  105.         expandListView.setAdapter(adapter);
  106.     }
  107. }

使用ExpandableAdapter,我们重新首先重写其中的一些方法,每一个getGroupView就是显示的每一个ListGroupItem(列表项),其中重写的getGroupCount方法放回的就是有多少列表项,而getChildView()就是显示每一个子列表项,同理getChildCount()每一个列表项里面的子列表项个数。

标签