本文介绍了ListView适配器getView()获得错误的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,并放入以下代码来查看BaseAdapter的getView()过程.

I have a ListView and I put in the following code to see the getView() process of my BaseAdapter.

@Override

public View getView(int position, View convertView, ViewGroup parent) {

    Log.d("getView()","Fetching Row: " + position);
    ...
}

当我缓慢向下滚动列表时,我注意到发生了一些奇怪的事情.即使我在列表位置40-50ish时,它仍在以0-7的位置调用getView().这是我的日志:

As I scroll down my list slowly, I notice something strange happening. Even when I'm at list position 40-50ish, it's still calling getView() with a position of 0-7. Here is what my log looks like:

05-14 11:46:13.989: I/getView()(18681): Fetching Row: 45
05-14 11:46:14.039: I/getView()(18681): Fetching Row: 0
05-14 11:46:14.049: I/getView()(18681): Fetching Row: 1
05-14 11:46:14.049: I/getView()(18681): Fetching Row: 2
05-14 11:46:14.049: I/getView()(18681): Fetching Row: 3
05-14 11:46:14.059: I/getView()(18681): Fetching Row: 4
05-14 11:46:14.059: I/getView()(18681): Fetching Row: 5
05-14 11:46:14.059: I/getView()(18681): Fetching Row: 6
05-14 11:46:14.069: I/getView()(18681): Fetching Row: 7
05-14 11:46:14.320: I/getView()(18681): Fetching Row: 46
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 0
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 1
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 2
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 3
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 4
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 5
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 6
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 7

如您所见,在第45行和第46行之间,它每次都为0-7行调用getView().每个列表项都会发生这种情况.恐怕这会导致性能问题.

As you can see, in between row 45 and row 46, it called getView() for row 0-7, each time. This happens for every list item. I'm afraid this is causing performance issues.

为什么会这样?这是正常行为吗?

Why is this happening? And is this normal behavior?

推荐答案

在列表视图xml中,将layout_height更改为android:layout_height="match_parent"

In your listview xml change the layout_height to android:layout_height="match_parent"

这篇关于ListView适配器getView()获得错误的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:22