本文介绍了另一个 getView 被多次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ListView中的每一项都包含一个ImageView和一个TextView,这将填充远程信息.

Each item in my ListView containt an ImageView and a TextView, this will be filled with remote information.

我获得了 ImageView 的 URL,因此我启动了一个 AsyncTask,它下载图像并使用下载的位图调用 setImageBitmap.

I got an URL for the ImageView, therefore I start an AsyncTask which downloads the image and will call setImageBitmap with the downloaded bitmap.

这很顺利,但是当创建 ListView 时,getView() 经常被调用.它对前 10 行(只有 7 行可见)调用了大约 7 倍的 getView.(所以:0、1 等,10、0、1 等).

This goes very well but when the ListView is created, the getView() is called to often.It calls about 7 times the getView for the first 10 rows (only 7 visible). (So: 0, 1, etc, 10, 0, 1 etc).

然后我可以平滑地滚动到第 10 项.但在那之后,对于每个新行,列表视图再次调用前 10 个项目的 getView 大约 7 倍.(这会导致延迟..)

Then I can just scroll smoothly to the 10th item. But after that, for each new row the listview calls again about 7 times the getView for the first 10 items. (This will cause lag..)

但是当我从 AsyncTask 中删除 setImageBitmap 时,这一切都不会发生!

But when I remove the setImageBitmap from the AsyncTask, this all won't happen!

可能是什么问题?会不会是一些布局过大导致再次出现 getViews ?

What could be the problem? Could it be that some layout is to big which will cause another streak of getViews ?

这里有一些代码:

<ListView
    android:id="@+id/mylist"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/mydivider"
    android:divider="@+color/mycolor"
    android:dividerHeight="2dp"
    android:fadingEdge="none"
    android:fastScrollEnabled="true"
    android:listSelector="@android:color/transparent"
    android:scrollbars="horizontal"
    android:scrollingCache="false"
    android:smoothScrollbar="false" />

异步任务:

public static class MyTask extends AsyncTask<String, Integer, Bitmap> {
    private LruCache<String, Bitmap> mMap;
    private String mUri;
    private int mPosition;
    private ViewHolder mHolder;

    public MyTask (ViewHolder holder, int position, LruCache<String, Bitmap> map) {
        mMap = map;
        mHolder = holder;
        mPosition = position;
    }

    @Override
    protected Bitmap doInBackground(String... url) {
        mUri = url[0];
        return getBitmapFromURL(mUri);
    }

    @Override
    protected void onPostExecute(Bitmap b) {
        if (b != null) {
            mMap.put(mUri, b);
            if (mPosition == mHolder.position) {
                holder.image.setImageBitmap(b);
            }
        }
    };
}

getView()

row = convertView;
ViewHolder holder;
if (row == null) {
    row = vi.inflate(R.layout.mylayout, null);
    holder = new ViewHolder();
    holder.image = (ImageView) row.findViewById(R.id.myimage);
    holder.title = (TextView) row.findViewById(R.id.mytext);
    row.setTag(holder);
} else {
    holder = (ViewHolder) row.getTag();
}

holder.title.setText("text");
holder.image.setImageResource(R.drawable.mydefaulticon);
holder.position = position;
startMyTask(content, holder, position);

更多信息:

创建新视图时,堆栈跟踪显示 getView 是从 ListView.makeAndAddView() 调用的但是在 getViews 的无用连胜中,它来自 ListView.measureHeigthOfChildren()

When an new view is created the stacktrace shown the getView was called from ListView.makeAndAddView()But in the useless streak of getViews it is coming from ListView.measureHeigthOfChildren()

所以当我设置位图时,布局似乎发生了变化......

So it seems like the layout is changed when I set the Bitmap...

推荐答案

问题出在ListView的布局上.

The problem was in the Layout of the ListView.

当我将参数layout_width 设置为wrap_content 时,我将其更改为fill_parent 问题消失了...

The parameter layout_width was set to wrap_content when I changed it to fill_parent the problem disappeared...

这篇关于另一个 getView 被多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:34