ListView优化-通用ViewHolder编写备份

ViewHolder.java

package cn.edu.bzu.util;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by monster on 2015/4/20.
* Have a good package ViewHolder
* get(Context context,View convertView,ViewGroup parent,int layoutId,int position) To make ViewHolder
*
*/
public class ViewHolder {
private SparseArray<View> mViews; //稀疏数组,SparseArray比HashMap执行效率高
private int mPosition;
private View mConvertView; //作用是方便系统在重写UI时,能重用原来实用过的View实例,以此来降低系统资源的消耗和提高代码效率
/*
*ConvertView的几点特征。
*对于一个ListView,Android保存所有曾经生成过的ConvertView实例,直至系统垃圾回收这些实例位置,而不是只保存最后使用的ConvertView对象。
*这些保存的ConvertView以使用时间顺序排序,并依次被传递到getView函数中。
*/
public ViewHolder(Context context,ViewGroup parent,int layoutId,int position){
//ViewHolder的构造方法
this.mPosition=position;
this.mViews=new SparseArray<View>(); //初始化SparseArray对象
mConvertView= LayoutInflater.from(context).inflate(layoutId,parent,false);
mConvertView.setTag(this);
} public static ViewHolder get(Context context,View convertView,ViewGroup parent,int layoutId,int position){
if(convertView==null){
return new ViewHolder(context,parent,layoutId,position);
}else{
ViewHolder holder=(ViewHolder)convertView.getTag();
holder.mPosition=position;
return holder;
}
} /**
* 通过viewId获取控件
* @param viewId
* @param <T>
* @return
*/
public <T extends View> T getView(int viewId){
View view=mViews.get(viewId);
if(view==null){
view=mConvertView.findViewById(viewId);
mViews.put(viewId,view);
}
return (T)view;
}
public View getConvertView() {
return mConvertView;
}
}

MyAdapterWithCommonViewHolder.java

package cn.edu.bzu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView; import java.util.List; import cn.edu.bzu.entity.Bean;
import cn.edu.bzu.listview_demo.R;
import cn.edu.bzu.util.ViewHolder; /**
* Created by monster on 2015/4/19.
*/
public class MyAdapterWithCommonViewHolder extends BaseAdapter {
private LayoutInflater mInflater; //LayoutInflater是用来找layout下xml布局文件,并且实例化
private List<Bean> mlist;
private Context mcontext;
public MyAdapterWithCommonViewHolder(Context context, List<Bean> list) {
mcontext=context;
mInflater=LayoutInflater.from(context); //得到初始化上下文
mlist=list;
}
@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) {
ViewHolder holder=ViewHolder.get(mcontext,convertView,parent,R.layout.listview_item,position);
((TextView)holder.getView(R.id.id_title)).setText(mlist.get(position).getTitle());
((TextView)holder.getView(R.id.id_desc)).setText(mlist.get(position).getDesc());
((TextView)holder.getView(R.id.id_time)).setText(mlist.get(position).getDate());
((TextView)holder.getView(R.id.id_phone)).setText(mlist.get(position).getPhone());
return holder.getConvertView();
} }

【源代码下载链接:https://github.com/monsterLin/ViewHolder_Package

上一篇:SQL2012数据库还原失败System.Data.SqlClient.SqlError: 无法执行 BACKUP LOG,因为当前没有数据库备份


下一篇:Hashtable insert failed. Load factor too high. The most common cause is multiple threads writing to the Hashtable simultaneously