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:
public class SimpleAdapter extends Activity{ private String[] names =new String[]{"nick","lebron","kobe","kevin"}; private int[] imgsIds[] =new Int[] {R.drawable.1,R.drawable.2,R.drawable.3,R.drawable.4}; @Override public void onCreate(Bundle savedInsatnceState) { super.onCreate(savedInsatnceState); setTitle("simpleadapter"); setContentView(R.layout.main); List<Map><String,Object> list=new ArrayList<Map><String,Object>();//创建动态数组ArrayList for(int i=0;i<names.length;i++){ Map<String,Object> map=new HashMap=<String,Object>; map.put("name",names[i]); map.put("headerImg",imgsIds[i]); list.add(map); } SimpleAdapter adapter=new SimpleAdaper(this,list,R.layout.main,new String[]{"name","headerImg"},new int[]{R.id.name,R.id.headerImg}); ListView list=(ListView)findViewById(R.id.mylist); list.setAdaper(adapter); } }
*注释*
SimpleAdapter是另一种常见的adapter类型,其有五个参数
1:Activity.this
2:传入的数据
3:数据传入的界面
4:数据中Map类型数据的String 类的key
5:穿入界面的组件的Id
**
第二种我们使用ArrayAdapter:代码不详解,只是重写Adapter:
String[] str={"china","america","japan"}; List list=new ArrayList<String>(); for(int i=0;i<str.length;i++){ list.add(str[i]); } 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:
public class ExpandableListViewTest extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建一个BaseExpandableListAdapter对象 ExpandableListAdapter adapter = new BaseExpandableListAdapter() { int[] logos = new int[] { R.drawable.p, R.drawable.z, R.drawable.t }; private String[] armTypes = new String[] { "神族兵种", "虫族兵种", "人族兵种"}; private String[][] arms = new String[][] { { "狂战士", "龙骑士", "黑暗圣堂", "电兵" }, { "小狗", "刺蛇", "飞龙", "自爆飞机" }, { "机枪兵", "护士MM" , "幽灵" } }; //获取指定组位置、指定子列表项处的子列表项数据 @Override public Object getChild(int groupPosition, int childPosition) { return arms[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public int getChildrenCount(int groupPosition) { return arms[groupPosition].length; } private TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView textView = new TextView(ExpandableListViewTest.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); return textView; } //该方法决定每个子选项的外观 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition).toString()); return textView; } //获取指定组位置处的组数据 @Override public Object getGroup(int groupPosition) { return armTypes[groupPosition]; } @Override public int getGroupCount() { return armTypes.length; } @Override public long getGroupId(int groupPosition) { return groupPosition; } //该方法决定每个组选项的外观 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(ExpandableListViewTest.this); ll.setOrientation(0); ImageView logo = new ImageView(ExpandableListViewTest.this); logo.setImageResource(logos[groupPosition]); ll.addView(logo); TextView textView = getTextView(); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); return ll; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean hasStableIds() { return true; } }; ExpandableListView expandListView = (ExpandableListView) findViewById(R.id.list); expandListView.setAdapter(adapter); } }
使用ExpandableAdapter,我们重新首先重写其中的一些方法,每一个getGroupView就是显示的每一个ListGroupItem(列表项),其中重写的getGroupCount方法放回的就是有多少列表项,而getChildView()就是显示每一个子列表项,同理getChildCount()每一个列表项里面的子列表项个数。