然而,另一个getView多次调用getView

2023-09-12 02:15:00 作者:这城市那么空这回忆那么凶

在我的的ListView containt的的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()被称为频繁。 它要求约7倍的 getView 的第10行(只有7可见)。 (所以: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项。但在那之后,每个新行的列表视图再次调用约7倍的 getView 的第10个项目。 (这将导致延迟。)

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..)

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

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 ?

下面一些code:

<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" />

在AsyncTask的:

The AsyncTask:

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()

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

一些详细信息:

Some more information:

当一个新的观点是从 ListView.makeAndAddView创建所示的getView的堆栈跟踪被称为() 但在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...

 
精彩推荐
图片推荐