在開發中,我們經常使用到ListView這個控制項。Android的API也提供了許多建立ListView適配器的捷徑。例如ArrayAdapter、SimpleAdapter和SimpleCursorAdapter等。但你是否發現,如果採用這些系統內建的適配器,對於事件的響應只能局限在一個行單位。假設一行裡面有一個按鈕和一個圖片控制項,它們之間的響應操作是不一樣的。若採用系統內建的適配器,就不能精確到每個控制項的響應事件。這時,我們一般採取自訂配接器來實現這個比較精確地請求。
ListView的建立,一般要具備兩大元素:
1)資料集,即要映射的字串、圖片資訊之類。
2)適配器,實現把要映射的字串、圖片資訊映射成視圖(如Textview、Image等組件),再添加到ListView中。
具體樣本:
1.建立適配器
public class UserAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return userList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return userList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_user, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.iconImg);
TextView tv = (TextView)convertView.findViewById(R.id.showName);
UserInfo user = userList.get(position);
try{
iv.setImageDrawable(user.getUserIcon());
tv.setText(user.getUserName());
}catch (Exception e){
e.printStackTrace();
}
return convertView;
}
}
2.ListView
View diaView = View.inflate(LoginActivity.this, R.layout.dialog2, null);
dialog = new Dialog(LoginActivity.this,R.style.dialog2);
dialog.setContentView(diaView);
dialog.show();
UserAdapter adapater = new UserAdapter();
ListView listview=(ListView)diaView.findViewById(R.id.list);
listview.setVerticalScrollBarEnabled(false);// ListView去掉下拉條
listview.setAdapter(adapater);
listview.setOnItemClickListener( new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
TextView tv=(TextView)arg1.findViewById(R.id.showName);
iconSelect.setText(tv.getText());
ImageView iv=(ImageView)arg1.findViewById(R.id.iconImg);
icon.setImageDrawable(iv.getDrawable());
dialog.dismiss();
}
});
}
布局檔案
dialog2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dip">
<ListView
android:id="@+id/list"
android:layout_width="240px"
android:layout_height="220px"
android:divider="#f1f2f2"
android:dividerHeight="1px"
android:layout_margin="5px"
android:background="#ffffff"
android:cacheColorHint="#00000000">
</ListView>
</LinearLayout>
item_user
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:id="@+id/iconImg"
android:layout_width="48dip"
android:layout_height="48dip" />
<TextView android:id="@+id/showName"
android:layout_gravity="center_vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>