Android開發(fā)學(xué)習(xí)教程(18)- Android ListView列表控件用法
—— 如果我這樣就倒下了,那么我也不過是這種程度的男人罷了。 ——索隆
ListView是什么
ListView是一個可以滑動的列表控件,當(dāng)我們需要以列表的方式顯示數(shù)據(jù)時,特別是大量的數(shù)據(jù)時,我們可以使用ListView控件,比如我們常見的通訊錄列表、微信聊天列表、朋友圈列表等。
ListView有什么用
ListView用來展示大量的數(shù)據(jù)列表時具有很好的靈活性和性能,這里需要提到的是,Android還提供了一個更靈活性和更高性能的列表控件RecycleView(這個在后面的篇章會詳細說明)。
ListView怎么用
1. ArrayAdapter
ArrayAdapter專門用來顯示只有一個TextView的數(shù)據(jù)列表項。這點可以從ArrayAdapter的源碼看出來。這里截取其中一小段說明:
final
?TextView text;
if
?(mFieldId ==?
0
) {
????
// 如果沒有指定顯示數(shù)據(jù)的控件,那就把整個布局view轉(zhuǎn)換成TextView
????
text = (TextView) view;
}?
else
?{
????
// 如果有指定,那就實例化
????
text = view.findViewById(mFieldId);
}
我們來看ArrayAdapter的具體用法,新建activity_main.xml:
<?
xml
?version
=
"1.0"
?encoding
=
"utf-8"
?>
<
androidx.constraintlayout.widget.ConstraintLayout
?xmlns:android
=
"http://schemas.android.com/apk/res/android"
????
xmlns:tools
=
"http://schemas.android.com/tools"
????
android:layout_width
=
"match_parent"
????
android:layout_height
=
"match_parent"
????
tools:context
=
".MainActivity"
>
????
<
ListView
????????
android:id
=
"@+id/listview"
????????
android:layout_width
=
"match_parent"
????????
android:layout_height
=
"match_parent"
????????
android:divider
=
"#EEEEEE"
????????
android:dividerHeight
=
"1dp"
?/>
</
androidx.constraintlayout.widget.ConstraintLayout
>
然后在MainActivity中實例化:
ListView listView = (ListView) findViewById(R.id.listview);
實例化了ListView之后給ListView裝載數(shù)據(jù),即實例化數(shù)據(jù)適配器Aadapter:
????
String[] data = {
"趙一"
,?
"劉二"
,?
"張三"
,?
"李四"
,?
"王五"
,?
"趙六"
,?
"韓梅梅"
,?
"李雷"
,?
"小明"
,?
"小紅"
,?
"小張"
,?
"小趙"
,?
"小劉"
,?
"小往"
,?
"小韓"
,?
"小李"
,?
"小孫"
,?
"小錢"
};
????
ArrayAdapter<string> array =?
new
?ArrayAdapter<>(
this
, android.R.layout.simple_list_item_1, data);
????
listView.setAdapter(array);
</string>
運行看效果:
2. SimpleAdapter
SimpleAdapter相對于ArrayAdpter展示只有一個TextView的數(shù)據(jù)列表項多了ImageView的數(shù)據(jù)列表項。這個看源碼也可以看得出。這里截取其中一小段說明:
if
?(v?
instanceof
?TextView) {
????
// Note: keep the instanceof TextView check at the bottom of these
????
// ifs since a lot of views are TextViews (e.g. CheckBoxes).
????
setViewText((TextView) v, text);
}?
else
?if
?(v?
instanceof
?ImageView) {
????
if
?(data?
instanceof
?Integer) {
????????
setViewImage((ImageView) v, (Integer) data);
????
}?
else
?{
????????
setViewImage((ImageView) v, text);
????
}
}
我們來看SimpleAdapter的具體用法,修改MainActivity代碼為:
????
ArrayList<map<string, string=
""
>> list =?
new
?ArrayList<>();
????
Map<string, string=
""
> hashMap =?
null
;
????
for
?(
int
?i =?
0
; i <?
20
; i++) {
????????
hashMap =?
new
?HashMap<>();
????????
hashMap.put(
"name"
,?
"第"
?+ i +?
"個item"
);
????????
list.add(hashMap);
????
}
????
SimpleAdapter simple =?
new
?SimpleAdapter(
this
, list, R.layout.item_listview,?
new
?String[]{
"name"
},?
new
?int
[]{R.id.tv_name});
????
listView.setAdapter(simple);
</string,></map<string,>
運行效果:
3. 自定義BaseAdaper
自定義BaseAdapter可以展示任意類型的數(shù)據(jù)列表項,其實上面SimpleAdapter和ArrayAdapter都是繼承自BaseAdapter,區(qū)別在于getView方法,SimpleAdapter和ArrayAdapter的getView讀者可以自行查看源碼,基本和上面截取的一小段差不多。我們來看自定義BaseAdapter的用法:
????
List<string> list =?
new
?ArrayList<>();
????
for
?(
int
?i =?
0
; i <?
20
; i++) {
??????????
list..add(
"第"
?+ i +?
"個item"
);
????
}
????
listView.setAdapter(
new
?MyAdapter(list));
</string>
其中MyAdapter類的代碼如下:
????
class
?MyAdapter?
extends
?BaseAdapter {
????????????
private
?List<string> mList;
????????????
public
?MyAdapter(List<string> mList) {
????????????????
this
.mList = mList;
????????????
}
????????????
@Override
????????????
public
?int
?getCount() {
????????????????
return
?mList ==?
null
???
0
?: mList.size();
????????????
}
????????????
@Override
????????????
public
?String getItem(
int
?position) {
????????????????
return
?mList.get(position);
????????????
}
????????????
@Override
????????????
public
?long
?getItemId(
int
?position) {
????????????????
return
?position;
????????????
}
????????????
@Override
????????????
public
?View getView(
int
?position, View convertView, ViewGroup parent) {
????????????????
ViewHolder holder =?
null
;
????????????????
//判斷是否緩存
????????????????
if
?(convertView ==?
null
) {
????????????????
holder =?
new
?ViewHolder();
????????????????
//通過LayoutInflater實例化布局
????????????????
convertView = mInflater.inflate(R.layout.item_listview,?
null
);
????????????????
holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
????????????????
convertView.setTag(holder);
????????????????
}?
else
?{
????????????????
//通過tag找到緩存的布局
????????????????
holder = (ViewHolder) convertView.getTag();
????????????????
}
????????????????
//設(shè)置布局中控件要顯示的視圖
????????????????
holder.tvName.setText(mList.get(position));
????????????????
return
?convertView;
????????????
}
????????????
public
?final
?class
?ViewHolder {
????????????????
public
?TextView tvName;
????????????
}
????????
}
????
}
</string></string>
運行效果:
源碼鏈接:https://yunjunet.cn/876782.html