平时生活中,刷新浪微博,感觉滑动的时候不够流畅(MX3),用过另一个App叫FuuBo,滑动的效果比新浪微博好太多了,简直丝滑.
今天学到convertView,做了个小测试:
在ListView中,当item较多,上下滑动的时候,不显示的item会被放入缓存中,如果去利用这些缓存的item,ListView的滑动效果会好很多!
Text Code(仅给出部分参考代码)::
不用缓存:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=View.inflate(MainActivity.this, R.layout.item,null);
Button username = (Button) view.findViewById(R.id.username);
Button password = (Button) view.findViewById(R.id.password);
Button delete = (Button) view.findViewById(R.id.delete);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);
username.setText(people.get(position).getUsername());
password.setText(people.get(position).getPassword());
checkBox.setSelected(people.get(position).isCheck());
delete.setTag(position);
delete.setOnClickListener(this);
return view;
}
使用缓存:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh = null;
if (convertView == null) {
vh = new ViewHolder();
convertView = View.inflate(MainActivity.this, R.layout.item,
null);
vh.username = (Button) convertView.findViewById(R.id.username);
vh.password = (Button) convertView.findViewById(R.id.password);
vh.delete = (Button) convertView.findViewById(R.id.delete);
vh.checkBox = (CheckBox) convertView
.findViewById(R.id.checkBox);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.username.setText(people.get(position).getUsername());
vh.password.setText(people.get(position).getPassword());
vh.checkBox.setSelected(people.get(position).isCheck());
vh.delete.setTag(position);
vh.delete.setOnClickListener(this);
return convertView;
}
把整个demo放到手机上测试的时候,差别非常明显!使用了缓存的demo就像女朋友的手~而没使用缓存的demo,简直就像我的手...........