Android必學(xué)-BaseAdapter的使用與優(yōu)化
http://www.imooc.com/learn/365
github源碼Android Studio2.3.1: https://github.com/kakukeme/Android_Learn/tree/master/ImoocBaseadapter
Android必學(xué)之?dāng)?shù)據(jù)適配器BaseAdapter
http://www.cnblogs.com/caobotao/p/5061627.html
第1章 ListView緩存機制
1-1 什么是BaseAdapter
Android 適配器模式使用;
數(shù)據(jù)適配器模式的應(yīng)用:
1、降低程序耦合性技竟;
2啦桌、容易擴(kuò)展;
1-2 ListView的顯示與緩存機制
需要才顯示辆它,顯示完就被回收到緩存;
第2章 BaseAdapter的逗比式
2-1 BaseAdapter之創(chuàng)建布局文件
activity_main 布局,就放個表格ListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.game.kokia.imoocbaseadapter.MainActivity">
<ListView
android:id="@+id/lv_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
item 布局, 使用RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_toEndOf="@+id/iv_image"
android:gravity="center"
android:text="Title"
android:textSize="25sp"
/>
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_toEndOf="@+id/iv_image"
android:layout_below="@+id/tv_title"
android:gravity="center_vertical"
android:text="Content"
android:textSize="20sp"
/>
</RelativeLayout>
2-2 BaseAdapter之創(chuàng)建數(shù)據(jù)源
Bean類软舌,(相當(dāng)于Model)
public class ItemBean {
public int ItemImageResId;
public String ItemTitle;
public String ItemContent;
public ItemBean(int itemImageResId, String itemTitle, String itemContent) {
ItemImageResId = itemImageResId;
ItemTitle = itemTitle;
ItemContent = itemContent;
}
}
MainActivity
的 onCreate
構(gòu)造假數(shù)據(jù)
List<ItemBean> itemBeenList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
itemBeenList.add(new ItemBean(
R.mipmap.ic_launcher,
"我是標(biāo)題" + i,
"我是內(nèi)容" + i
));
}
2-3 BaseAdapter之?dāng)?shù)據(jù)適配器初解
通過構(gòu)造方法,將數(shù)據(jù)源與數(shù)據(jù)適配器進(jìn)行關(guān)聯(lián)牛曹;
返回ListView需要顯示的數(shù)據(jù)數(shù)量
返回指定索引對應(yīng)的數(shù)據(jù)項
對應(yīng)的索引項
返回每一項的顯示內(nèi)容
public class MyAdapter extends BaseAdapter {
private List<ItemBean> mList;
private LayoutInflater mInflater;
// 通過構(gòu)造方法佛点,將數(shù)據(jù)源與數(shù)據(jù)適配器進(jìn)行關(guān)聯(lián);
public MyAdapter(Context context, List<ItemBean> list) {
mList = list;
mInflater = LayoutInflater.from(context);
}
// 返回ListView需要顯示的數(shù)據(jù)數(shù)量
@Override
public int getCount() {
return mList.size();
}
// 返回指定索引對應(yīng)的數(shù)據(jù)項
@Override
public Object getItem(int position) {
return mList.get(position);
}
// 對應(yīng)的索引項
@Override
public long getItemId(int position) {
return position;
}
// 返回每一項的顯示內(nèi)容
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = mInflater.inflate(R.layout.item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);
TextView title = (TextView) view.findViewById(R.id.tv_title);
TextView content = (TextView) view.findViewById(R.id.tv_content);
ItemBean bean = mList.get(position);
imageView.setImageResource(bean.ItemImageResId);
title.setText(bean.ItemTitle);
content.setText(bean.ItemContent);
return view;
}
}
2-4 BaseAdapter之逗比式
1黎比、創(chuàng)建view
2超营、使用LayoutInflater 將布局xml文件轉(zhuǎn)換為view布局;
context對LayoutInflater進(jìn)行初始化
context:要使用當(dāng)前的Adapter的界面對象
mInflater: 布局裝載器對象阅虫;
getView每次都創(chuàng)建新的view演闭;沒有利用到ListView的緩存機制;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<ItemBean> itemBeenList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
itemBeenList.add(new ItemBean(
R.mipmap.ic_launcher,
"我是標(biāo)題" + i,
"我是內(nèi)容" + i
));
}
ListView listView = (ListView) findViewById(R.id.lv_main);
listView.setAdapter(new MyAdapter(this, itemBeenList));
}
}
- BaseAdapter 的三重境界之逗比式
沒有任何優(yōu)化處理颓帝,每次都創(chuàng)建新的View米碰,設(shè)置控件窝革;
不解釋,效率極其低下
3-1 BaseAdapter之?dāng)?shù)據(jù)適配器詳解-普通式
- BaseAdapter 的三重境界之普通式
利用了ListView的緩存特性吕座,如果沒有緩存才創(chuàng)建新的View
算入門虐译,但是findViewById依然會浪費大量時間
convertView 是否緩存過了,
// 為空:View未實例化吴趴,緩存池中無緩存漆诽;需要創(chuàng)建;
// 返回每一項的顯示內(nèi)容
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 逗比式===============================
/*
View view = mInflater.inflate(R.layout.item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);
TextView title = (TextView) view.findViewById(R.id.tv_title);
TextView content = (TextView) view.findViewById(R.id.tv_content);
// 設(shè)置數(shù)據(jù)
ItemBean bean = mList.get(position);
imageView.setImageResource(bean.ItemImageResId);
title.setText(bean.ItemTitle);
content.setText(bean.ItemContent);
return view;
*/
// 逗比式===============================
// 普通式===============================
// 為空:View未實例化史侣,緩存池中無緩存拴泌;需要創(chuàng)建;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item, null);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_image);
TextView title = (TextView) convertView.findViewById(R.id.tv_title);
TextView content = (TextView) convertView.findViewById(R.id.tv_content);
ItemBean bean = mList.get(position);
imageView.setImageResource(bean.ItemImageResId);
title.setText(bean.ItemTitle);
content.setText(bean.ItemContent);
return convertView;
// 普通式===============================
}
4-1 文藝式
1惊橱、避免了重復(fù)創(chuàng)建大量的contentView
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item, null);
}
2蚪腐、創(chuàng)建內(nèi)部類ViewHolder
- 創(chuàng)建內(nèi)部類ViewHolder
- 判斷convertView是否為空
- 通過setTag將ViewHolder與convertView綁定
- 通過getTag取出關(guān)聯(lián)的ViewHolder
- 功能通過ViewHolder對象找到對應(yīng)控件(避免重復(fù)的findViewById)
// 返回每一項的顯示內(nèi)容
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 逗比式===============================
/*View view = mInflater.inflate(R.layout.item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);
TextView title = (TextView) view.findViewById(R.id.tv_title);
TextView content = (TextView) view.findViewById(R.id.tv_content);
// 設(shè)置數(shù)據(jù)
ItemBean bean = mList.get(position);
imageView.setImageResource(bean.ItemImageResId);
title.setText(bean.ItemTitle);
content.setText(bean.ItemContent);
return view; */
// 逗比式===============================
// 普通式===============================
// 為空:View未實例化,緩存池中無緩存税朴;需要創(chuàng)建回季;
/*if (convertView == null) {
convertView = mInflater.inflate(R.layout.item, null);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_image);
TextView title = (TextView) convertView.findViewById(R.id.tv_title);
TextView content = (TextView) convertView.findViewById(R.id.tv_content);
ItemBean bean = mList.get(position);
imageView.setImageResource(bean.ItemImageResId);
title.setText(bean.ItemTitle);
content.setText(bean.ItemContent);
return convertView;*/
// 普通式===============================
// 文藝式===============================
ViewHolder viewHolder;
if (convertView = null) {
convertView = mInflater.inflate(R.layout.item, null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_image);
viewHolder.title = (TextView) convertView.findViewById(R.id.tv_title);
viewHolder.content = (TextView) convertView.findViewById(R.id.tv_content);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
ItemBean bean = mList.get(position);
viewHolder.imageView.setImageResource(bean.ItemImageResId);
viewHolder.title.setText(bean.ItemTitle);
viewHolder.content.setText(bean.ItemContent);
// 文藝式===============================
}
// 避免重復(fù)的findViewById
class ViewHolder {
public ImageView imageView;
public TextView title;
public TextView content;
}
- BaseAdapter 的三重境界之文藝式
不僅利用了ListView的緩存,更通過ViewHolder類來實現(xiàn)顯示數(shù)據(jù)的視圖的緩存正林,避免多次通過findViewById尋找控件
作為一個有情懷的程序員泡一,這才是最文藝的寫法;
5-1 深入講解
獲取系統(tǒng)納秒時間觅廓;
5-2 總結(jié)
ViewHolder 優(yōu)化 BaseAdapter 思路:
- 創(chuàng)建Bean對象鼻忠,用于封裝數(shù)據(jù)
- 在構(gòu)造方法中初始化用于映射的數(shù)據(jù)List
- 創(chuàng)建ViewHolder類,創(chuàng)建布局映射關(guān)系
- 判斷convertView杈绸,為空則創(chuàng)建帖蔓,并設(shè)置tag,否則通過tag來取出ViewHolder
- 給ViewHolder中的控件設(shè)置數(shù)據(jù)