ListView的优化
在没有优化之前,每当显示一个屏幕的ListView的Item时都会执行Adapter的getView方法几次,反复被调用,导致越划越卡
- 优化每次都加载布局文件(对视图的优化)
- 优化每次都findViewbyid()找组件
- 让每个ListView里的Item长的不相同
-
在getView方法中第二个参数convertView就是用来缓存布局文件的,当显示完一屏的时候,在显示下一个ITem时,第一个Item离开屏幕,并缓存给要显示的Item。所以我们可以如下方法来加载需要的布局:
if (convertView == null) {
convertView=View.inflate(MainActivity.this,
R.layout.myitem_adpter, null); } -
以上虽然对视图进行了优化,不用每次都重新加载,但是每次都得findviewById。我们先需要定义内部static类
static class ViewHolder {
TextView textView;
}
定义静态可以保证只加载一次,并且优先加载
然后通过如下代码放到加载布局文件的那个if语句中
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) convertView
.findViewById(R.id.iv_item);
convertView.setTag(viewHolder);
然后在这个else代码中设置
else { viewHolder = (ViewHolder) convertView.getTag(); }
自定义ListView的万能Adapter
定义ViewHolder类
public class ViewHolder {
private Context mContext;
private int mPosition;
private View mconvertView;
private SparseArray<View> mViews = new SparseArray<>();
public ViewHolder(int position, View convertView, ViewGroup parent,
int resId, Context context) {
convertView = LayoutInflater.from(context)
.inflate(resId, parent, false);
mContext = context;
mPosition = position;
mconvertView = convertView;
convertView.setTag(this);
}
public static ViewHolder get(int position, View convertView,
ViewGroup parent, int resId, Context context) {
if (convertView == null) {
return new ViewHolder(position, convertView, parent, resId, context);
} else {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
return viewHolder;
}
}
public <T extends View> T getView(int resId) {
View view = mViews.get(resId);
if (view == null) {
view = mconvertView.findViewById(resId);
mViews.put(resId, view);
}
return (T) view;
}
public View getmConvertView() {
return mconvertView;
}
}
在自己写个类继承BaseAdapter
public abstract class CommonAdapter<T> extends BaseAdapter {
protected List<T> mDatas;
protected Context mContext;
protected int mResId;
public CommonAdapter(Context context, List<T> data, int ResId) {
mDatas = data;
mContext = context;
mResId = ResId;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holde = ViewHolder.get(position, convertView, parent,
mResId, mContext);
current(holde, getItem(position));
return holde.getmConvertView();
}
public abstract void current(ViewHolder holder, Object object);
}
自己写的类只需要继承CommonAdapter就行重写current方法
public class OmnipotentAdapter extends CommonAdapter<Bean> {
public OmnipotentAdapter(Context context, List<Bean> data, int ResId) {
super(context, data, ResId);
}
@Override
public void current(ViewHolder holder, Object object) {
Bean bean = (Bean) object;
((TextView) holder.getView(R.id.tv_title)).setText(bean.getTitle());
((TextView) holder.getView(R.id.tv_desc)).setText(bean.getDesc());
((TextView) holder.getView(R.id.tv_time)).setText(bean.getTime());
((TextView) holder.getView(R.id.tv_num)).setText(bean.getPhone());
}
}