很简单的总结下listView的优化方案,供新手参考,高手勿喷,有错误的地方希望大家指正,谢谢啦!闲话就说到这里啦,接下来进入主题
首先想到的就是重用view,这样可以减少内存的消耗,同时加快Item加载速度,我总结了三种优化方法:
1:重用convertView,减少内存消耗
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHoler holer;
if (convertView == null) {
holer = new ViewHoler();
//R.layout.list_item这个布局是需要适配的Item的数据
convertView = inflater.inflate(R.layout.list_item, null);
//ID,Name是R.layout.list_item布局中的控件
holer.Name = (TextView) convertView.findViewById(R.id.Name);
holer.ID = (TextView) convertView.findViewById(R.id.ID);
convertView.setTag(holer);
} else {
holer = (ViewHoler) convertView.getTag();
}
//这里可以对上面的两个控件进行设置值
holer.Name.setText("name");
holer.ID.setText("ID");
return convertView;
}
2:设置内部类减少查找控件的ID,减少性能消耗
private static class ViewHoler {
public TextView Name;
public TextView ID;
}
3:
使用了内部类classViewHolder、重用了convertView。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHoler holer;
if (convertView == null) {
holer = new ViewHoler();
//R.layout.list_item这个布局是需要适配的Item的数据
convertView = inflater.inflate(R.layout.list_item, null);
//ID,Name是R.layout.list_item布局中的控件
holer.Name = (TextView) convertView.findViewById(R.id.Name);
holer.ID = (TextView) convertView.findViewById(R.id.ID);
convertView.setTag(holer);
} else {
holer = (ViewHoler) convertView.getTag();
}
//这里可以对上面的两个控件进行设置值
holer.Name.setText("name");
holer.ID.setText("ID");
return convertView;
}
private static class ViewHoler {
public TextView Name;
public TextView ID;
}
最后附上完整的代码,仅供参考,希望大家多多指教哦
import java.util.ArrayList;
import java.util.List;
import library.utils.IsNullUtils;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
//这个是你需要适配的数据,list集合,Object是你需要适配的任何类型的数据,包括对象
private List<Object> myList;
private Activity activity;
private LayoutInflater inflater;
public MyAdapter(Activity activity) {
this.activity = activity;
myList = new ArrayList<Object>();
inflater = LayoutInflater.from(activity);
}
/**
* 添加需要适配器的数据
* @param allObject
*/
public void add(List<Object> allObject) {
if (!IsNullUtils.isNull(myList)) {
myList = new ArrayList<Object>();
}
myList.addAll(allObject);
notifyDataSetChanged();
}
@Override
public int getCount() {
return myList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHoler holer;
if (convertView == null) {
holer = new ViewHoler();
//R.layout.list_item这个布局是需要适配的Item的数据
convertView = inflater.inflate(R.layout.list_item, null);
//ID,Name是R.layout.list_item布局中的控件
holer.Name = (TextView) convertView.findViewById(R.id.Name);
holer.ID = (TextView) convertView.findViewById(R.id.ID);
convertView.setTag(holer);
} else {
holer = (ViewHoler) convertView.getTag();
}
//这里可以对上面的两个控件进行设置值
holer.Name.setText("name");
holer.ID.setText("ID");
return convertView;
}
private static class ViewHoler {
public TextView Name;
public TextView ID;
}
}