Android必学-BaseAdapter的使用与优化
http://www.imooc.com/learn/365
github源码Android Studio2.3.1: https://github.com/kakukeme/Android_Learn/tree/master/ImoocBaseadapter
Android必学之数据适配器BaseAdapter
http://www.cnblogs.com/caobotao/p/5061627.html
第1章 ListView缓存机制
1-1 什么是BaseAdapter
Android 适配器模式使用;
数据适配器模式的应用:
1、降低程序耦合性;
2、容易扩展;
1-2 ListView的显示与缓存机制
需要才显示,显示完就被回收到缓存;
第2章 BaseAdapter的逗比式
2-1 BaseAdapter之创建布局文件
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之创建数据源
Bean类,(相当于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
构造假数据
List<ItemBean> itemBeenList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
itemBeenList.add(new ItemBean(
R.mipmap.ic_launcher,
"我是标题" + i,
"我是内容" + i
));
}
2-3 BaseAdapter之数据适配器初解
通过构造方法,将数据源与数据适配器进行关联;
返回ListView需要显示的数据数量
返回指定索引对应的数据项
对应的索引项
返回每一项的显示内容
public class MyAdapter extends BaseAdapter {
private List<ItemBean> mList;
private LayoutInflater mInflater;
// 通过构造方法,将数据源与数据适配器进行关联;
public MyAdapter(Context context, List<ItemBean> list) {
mList = list;
mInflater = LayoutInflater.from(context);
}
// 返回ListView需要显示的数据数量
@Override
public int getCount() {
return mList.size();
}
// 返回指定索引对应的数据项
@Override
public Object 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) {
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、创建view
2、使用LayoutInflater 将布局xml文件转换为view布局;
context对LayoutInflater进行初始化
context:要使用当前的Adapter的界面对象
mInflater: 布局装载器对象;
getView每次都创建新的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,
"我是标题" + i,
"我是内容" + i
));
}
ListView listView = (ListView) findViewById(R.id.lv_main);
listView.setAdapter(new MyAdapter(this, itemBeenList));
}
}
- BaseAdapter 的三重境界之逗比式
没有任何优化处理,每次都创建新的View,设置控件;
不解释,效率极其低下
3-1 BaseAdapter之数据适配器详解-普通式
- BaseAdapter 的三重境界之普通式
利用了ListView的缓存特性,如果没有缓存才创建新的View
算入门,但是findViewById依然会浪费大量时间
convertView 是否缓存过了,
// 为空:View未实例化,缓存池中无缓存;需要创建;
// 返回每一项的显示内容
@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;
*/
// 逗比式===============================
// 普通式===============================
// 为空:View未实例化,缓存池中无缓存;需要创建;
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、避免了重复创建大量的contentView
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item, null);
}
2、创建内部类ViewHolder
- 创建内部类ViewHolder
- 判断convertView是否为空
- 通过setTag将ViewHolder与convertView绑定
- 通过getTag取出关联的ViewHolder
- 功能通过ViewHolder对象找到对应控件(避免重复的findViewById)
// 返回每一项的显示内容
@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; */
// 逗比式===============================
// 普通式===============================
// 为空:View未实例化,缓存池中无缓存;需要创建;
/*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);
// 文艺式===============================
}
// 避免重复的findViewById
class ViewHolder {
public ImageView imageView;
public TextView title;
public TextView content;
}
- BaseAdapter 的三重境界之文艺式
不仅利用了ListView的缓存,更通过ViewHolder类来实现显示数据的视图的缓存,避免多次通过findViewById寻找控件
作为一个有情怀的程序员,这才是最文艺的写法;
5-1 深入讲解
获取系统纳秒时间;
5-2 总结
ViewHolder 优化 BaseAdapter 思路:
- 创建Bean对象,用于封装数据
- 在构造方法中初始化用于映射的数据List
- 创建ViewHolder类,创建布局映射关系
- 判断convertView,为空则创建,并设置tag,否则通过tag来取出ViewHolder
- 给ViewHolder中的控件设置数据