Android:ListAdapter 示例重绘相同内容

标签 android listadapter

我使用这个例子中的 ListAdapter:

http://code.google.com/p/au-optimizing-layouts-201/source/browse/au_optimizinglayouts/src/com/example/android/training/optimizinglayouts/OptimizedListAdapter.java?r=4

这个例子不是很好,因为列表中的元素会根据我滚动的速度绘制多次。那是因为只复用了一个

private FakeImageLoader mFakeImageLoader;

如果 FakeImageLoader.java getImage() 将加载真实图像。 要运行此示例并保持性能,需要做什么?

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;
        }

谢谢塔塔

最佳答案

我用了这个universal image loader library在一个应用程序中下载图像的通用图像加载器库,对我来说更有用。

关于Android:ListAdapter 示例重绘相同内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14568554/

相关文章:

android - Flutter aduioplayers MissingPluginException(未在 channel xyz.luan/audioplayers 上找到方法 startHeadlessService 的实现)

android - 如何在Android通知中创建可点击的Textview?

java - 具有实时传感器更新的android listview

java - 如何在 AsyncTask 中使用 ListAdapter?

android - 图像适配器泄漏内存

Android,如何增加列表适配器中的行高?

android - 列表适配器 : Json data to be sent to spinner in android

android - 为 Ionic 构建阻止了 HTTPS --release android apk

android相机获取光传感器值

Android:计划重复的 TimerTask 仅被触发一次