擴充卡
擴充卡簡單點說,就是把資料源中的每一個元素按某種布局方式垂直排列,說到底還是一個布局
所有需要擴充卡的進階元件,都有一個 setAdapter的方法,是以擴充卡一般配合setAdapter使用
ArrayAdapter
ArrayAdapter 隻能關聯一個元件
///自定義一個ArrayAdapter
ArrayAdapter<T> adapter = new ArrayAdapter<T>(this,R.layout.autoadaptor, R.id.tv1, names);
//this :上下文
//R.layout.autoadaptor : 資料源要使用的布局
//R.id.tv1 : 添加到布局的哪個元件
//names :資料源
//系統ArrayAdapter
ArrayAdapter<T> adapter = new ArrayAdapter<T>(this,android.R.layout.simple_list_item_1, names);
SimpleAdapter
資料源:List<HashMap<String , Object>>
構造方法:
SimpleAdapter adapter = new SimpleAdapter(this , getData() , R.layout.item_person , from , to);
//存放的是在布局中所有的需要設定值的元件的id
int[] to = {R.id.img};
//在HashMap中,所有的鍵的值
String[] from = {"img"};
//1對1 映射關系 兩個數組的每一位都是關聯在一起的
List的長度是條目的個數
HashMap 是每個條目的資料
在Activity_main中建立ListView元件
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
//建立一個xml模版muban.xml
<?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"
android:padding="10dp" >
<ImageView
android:id="@+id/img"
android:layout_width="150dp"
android:layout_height="150dp"
android:adjustViewBounds="true"
android:src="@drawable/zhaoyun" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:singleLine="true"
android:text="趙雲"
android:textSize="24sp" />
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="趙雲(?-229年),字子龍,常山真定(今河北省正定)人。身長八尺,姿顔雄偉,三國時期蜀漢名将。" />
</LinearLayout>
</LinearLayout>
//在MainActivity中建立SimpleAdapter
SimpleAdapter adapter = new SimpleAdapter(this , list , muban.xml , from , to);
//建立list , from , to
//from:key值數組(String類型),to:布局空間id數組(int類型))
List<HashMap<String , Object>> list = new List<HashMap<String , Object>>();
String[] key = {"img" , "name" ,"info"}; //存放的是布局中所有需要設定的元件
int[] view = {"R.id.img" , "R.id.name" , "R.id.info"};
public List<HashMap<String , Object>> getData(){
//HashMap 中有多少個key,說明需要設定在一個條目中視圖的個數
HashMap<String , Object> zhaoyun = new HashMap<String , Object>();
zhaoyun.put("img" , R.drawable/zhaoyun);
zhaoyun.put("name" , "趙雲");
zhaoyun.put("info" , "趙雲(?-229年),字子龍,常山真定(今河北省正定)人。身長八尺,姿顔雄偉,三國時期蜀漢名将。");
list.add(zhaoyun);
HashMap<String , Object> machao = new HashMap<String , Object>();
machao.put("img" , R.drawable/machao);
machao.put("name" , "馬超");
machao.put("info" , "馬超(176年-222年),字孟起,扶風茂陵人,漢伏波将軍馬援的後人,馬騰的兒子,少年成名。");
list.add(machao);
return list;
}
BaseAdapter 最常用的擴充卡List
萬能擴充卡,什麼都可以做
在Activity給一個ListView
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
findViewById
ListView lv = (ListView) findViewById(R.id.lv);
為item去做布局muban.xml
加載資料
class User{
int imgId;
String name;
String info;
public User(int imgId, String name, String info) {
super();
this.imgId = imgId;
this.name = name;
this.info = info;
}
}
List<User> users = new ArrayList<User>();
users.add(new User(R.drawable.zhaoyun, "趙雲", "常山真定人"));
users.add(new User(R.drawable.machao, "馬超", "扶風茂陵人"));
users.add(new User(R.drawable.zhouyu, "周瑜", "廬江舒人"));
設計擴充卡
class MyBaseAdapter extends BaseAdapter {
@Override
public int getCount() {
// 整個資料的長度
return users.size();
}
@Override
public Object getItem(int position) {
// 擷取目前的item
return position;
}
@Override
public long getItemId(int position) {
// 傳回position
return ;
}
// 每一條資料對應的布局
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// position:目前整個布局是 第幾條item
View layout = View.inflate(getBaseContext(), R.layout.list, null);
ImageView img = (ImageView) layout.findViewById(R.id.img);
TextView name = (TextView) layout.findViewById(R.id.name);
TextView info = (TextView) layout.findViewById(R.id.info);
User user = users.get(position);
img.setImageResource(user.imgId);
name.setText(user.name);
info.setText(user.info);
return layout;
}
}
給ListView設定擴充卡
lv.setAdapter(new MyBaseAdapter());
//布局重用