问题描述
我用ListAdapter从这个例子:
I use a ListAdapter from this example:
<一个href=\"http://$c$c.google.com/p/au-optimizing-layouts-201/source/browse/au_optimizinglayouts/src/com/example/android/training/optimizinglayouts/OptimizedListAdapter.java?r=4\" rel=\"nofollow\">http://$c$c.google.com/p/au-optimizing-layouts-201/source/browse/au_optimizinglayouts/src/com/example/android/training/optimizinglayouts/OptimizedListAdapter.java?r=4
这个例子是不是很好,因为在列表中的元素拿得出好几次我取决于如何快速滚动。因为只有一个复用的那
The example is not very good, because elements in the list get drawn several times depending how fast I scroll. Thats because of the reuse of only one
private FakeImageLoader mFakeImageLoader;
如果FakeImageLoader.java的getImage()将加载一个真正的形象。
需要采取哪些措施来获得这个例子中运行,并保持高性能?
If FakeImageLoader.java getImage() would load a real image.What needs to be done to get this example running and stay performant?
public class OptimizedListAdapter extends BaseAdapter{
...
private FakeImageLoader mFakeImageLoader;
public OptimizedListAdapter(Context cxt)
{
// Cache the LayoutInflate to avoid asking for a new one each time.
mLayoutInflater = LayoutInflater.from(cxt);
mFakeImageLoader = new FakeImageLoader(cxt);
...
public View getView(final int position, View convertView, ViewGroup parent)
{
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if(convertView==null)
{
convertView = mLayoutInflater.inflate(R.layout.listitem, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
convertView.setTag(holder);
}else
{
holder = (ViewHolder) convertView.getTag();
}
//holder.icon.setImageDrawable(drawable);
holder.text.setText(new StringBuffer(ITEM_PRE_TEXT).append(position));
holder.timestamp.setText(""+(System.currentTimeMillis()-START_TIMESTAMP));
holder.position = position;
//during a fast scroll this could start 100's of threads, need a thread pool processing them!
//see previous ImageLoader.
new AsyncTask<ViewHolder, Void, Bitmap>() {
private ViewHolder v;
@Override
protected Bitmap doInBackground(ViewHolder... params) {
v = params[0];
return mFakeImageLoader.getImage();
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if(v.position == position)
{
v.icon.setImageBitmap(result);
}
}
}.execute(holder);
//holder.icon.setImageBitmap(mFakeImageLoader.getImage());
return convertView;
}
由于塔塔
推荐答案
我用这个通用映像加载库下载图像,这是比较有用的我。
I used this universal image loader library universal image loader library in one app to download images and it is more useful for me.
这篇关于安卓:ListAdapter例如重绘相同的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!