ListViews在屏幕上看到之前向 s 询问Adapters 。Viewan 的默认实现Adapter实际上会在ListView请求时夸大您的视图。
使用 an 时Object extends ArrayAdapter<MyObject>,我们实际上可以将所有视图存储在 aHashMap<MyObject, SoftReference<View>>和 override中ArrayAdapter.getView,因此我们只能膨胀Views一次,但如果GarbageCollectorpass 和 make our Views null(或 mySoftReferences是null出于任何奇怪的原因制作的)我们会再次膨胀它们。
@Override
public View getView(final int position, final View convertView,
final ViewGroup parent)
{
final MyObject mo = getItem(position);
if (hashMap.get(mo) == null || hashMap.get(mo).get() == null)
{
final MyCustomView mcv = new MyCustomView(getContext());
hashMap.put(mo, new SoftReference<MyObject>(mcv));
}
return hashMap.get(mo).get();
}
我试过了,效果很好。是否以任何方式不鼓励这种实施?