Android常用组件之ListView
使用ListAdapter是ListView的适配器。
使用ListView.setAdapter()方法来设置ListView的适配器。
- 简单的使用
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1); adapter.add("hello"); adapter.add("eoe"); lv.setAdapter(adapter); |
R前面的android表示使用的是系统提供给我们的资源。
- 使用自定义的资源
在layout包下新建一个XML,内容如下:
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> |
然后如下所示,创建Adapter:
adapter = new ArrayAdapter<String>(this,R.layout.list_cell_view); adapter.add("hello"); adapter.add("eoe"); lv.setAdapter(adapter); |
- 自定义Adapter
private BaseAdapter adapter = new BaseAdapter() { ????????private String[] data = {"user1","user2","user3","user4","user5","user6", ????"user7","user8","user9","user10","user11","user12","user13","user14",}; ????@Override ????public View getView(int position, View convertView, ViewGroup parent) { ????????TextView tv = new TextView(MainActivity.this); ????????tv.setText(getItem(position)); ????????return tv; ????} ???????? ????@Override ????public long getItemId(int position) { ????????return position; ????} ???????? ????@Override ????public String getItem(int position) { ????????return data[position]; ????} ???????? ????@Override ????public int getCount() { ????????return data.length; ????} }; |
程序运行时,界面上显示多少个列表项,getView方法就是被执行多少次。
getView参数中的View表示被系统回收的View。
优化:
TextView tv = null; if (convertView!=null){ ????tv = (TextView) convertView; }else { ????tv = new TextView(MainActivity.this); } tv.setText(getItem(position)); |
- 创建一个可以显示图片的ListView
在layout中新建一个Linearlayout布局的文件:
<?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="horizontal" > <ImageView android:layout_width="120dp" android:layout_height="120dp" android:id="@+id/icon"/> <LinearLayout android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/dec" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> ? </LinearLayout> |
创建一个Adapter:
private BaseAdapter adapter = new BaseAdapter() { private ListCellData[] data = new ListCellData[]{ ????new ListCellData("小明",???? "男", android.R.drawable.ic_btn_speak_now), ????new ListCellData("小丽",???? "女", android.R.drawable.ic_dialog_dialer), }; ????@Override ????public View getView(int position, View convertView, ViewGroup parent) { ????????LinearLayout line = null; ????????if (convertView!=null){ ????????????line = (LinearLayout) convertView; ????????}else { ????????????line = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.list_cell_view, null); ????????} ????????ListCellData item = getItem(position); ????????ImageView image = (ImageView) line.findViewById(R.id.icon); ????????TextView name = (TextView) line.findViewById(R.id.name); ????????TextView dec = (TextView) line.findViewById(R.id.dec); ????????image.setImageResource(item.getIcon()); ????????name.setText(item.getUsername()); ????????dec.setText(item.getSex()); ????????return line; ????} ???????? ????@Override ????public long getItemId(int position) { ????????return position; ????} ???????? ????@Override ????public ListCellData getItem(int position) { ????????return data[position]; ????} ???? ????@Override ????public int getCount() { ????????return data.length; ????} }; |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。