且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用多个子项的ListView

更新时间:2023-11-29 17:32:46

POJO

POJO实际上是一个Java编程术语,而不一定只与Android有关。它代表着普通Java对象和其他很多语言都有自己的等价物的缩写。 ***有一个很好的写一下吧。这个想法是需要一个非常简单的类来绕过一些数据。在您正在使用一个POJO的例子。

POJO is actually a java programming term and not necessarily associated only with Android. It stands for Plain Old Java Object and many other languages have their own equivalent acronym. Wikipedia has a nice write up about it. The idea is needing a very simple class to pass around some data. The Holder you are using is an example of a POJO.

计数

至于你提到你的问题的一部分,是了解如何在 BaseAdapter 的工作稍有不慎。我假设你已经定义如下:

As you mentioned, part of your problem is an slight mistake in understanding how a BaseAdapter works. I'm assuming you had defined the following:

@Override
public int getCount() {
    return scaledImage.length();
}

当在现实中你需要的东西更像是:

When in reality you needed something more like:

@Override
public int getCount() {
    //This assumes you'll ensure the array will always be equally divisible by 4.
    //If not, you'll need to add some empty elements at the end to make it so.
    return scaledImage.length() / 4;
}

适配器只知道它包含的行数的和不如何在总的数据量被用于行。接收 IndexOutOfBoundsExpections 是不使用的结果 scaledImage 作为您code所示。它是在别处所引起的问题。使用数组一样,是完全正常的。所以,只要你的 getCount将()方法返回正确的值。

The adapter is only aware of the number of rows it contains and not how much data in total is used for the rows. Receiving IndexOutOfBoundsExpections is not the result of using scaledImage as shown in your code. It's a problem caused elsewhere. Using an array like that is perfectly fine...so long as your getCount() method is returning the correct value.

应用POJO

由于Chitrang曾建议,则可以将 scaledImage 数组转换成一个POJO。因此,例如,你必须是这样的:

As Chitrang has suggested, you could convert your scaledImage array into a POJO. So for example, you'd have something like:

private class ScaledImageRow {
    //You could just as easily use an array of 4 elements instead
    Drawable image1;
    Drawable image2;
    Drawable image3;
    Drawable image4;
}

然后,当你正在构建自定义适配器,您会希望您的转换:

Then when you are constructing your custom adapter, you'd want to convert your:

Drawable[] scaledImage;

ScaledImageRow[] scaledImageRow;

这样,你就可以返回 scaledImageRow 的长度为计数,并在 getView()通过使用获得的图像正确的行 scaledImageRow [位置]

By doing this, you can then return the length of the scaledImageRow for the count and in your getView() obtained the correct row of images by using scaledImageRow[position]

现在在转换数据以这样的POJO由你什么明智的性能并没有真正的。它所做的更多的所以增加code的可读性。这也将有助于概念化发生了什么事情与你的code更好。

Now converting your data over to such a POJO doesn't really by you anything performance wise. What it does more so is increase the readability of your code. It will also help conceptualize what's going on with your code better.

性能

在回答描绘你提到你的 getView()一些非常慢的性能。阵列的方法绝对不是它的原因。我会说这是图像的设置。我猜你已经加载图像到内存中。因此,或许图像仍然相当大,在的ImageView 有某种比例在XML定义的属性呢?否则,我想说的放缓正在您的code在其他地方造成的。

You mentioned some very slow performance with your getView() as depicted in answer. Your array approach is definitely not the cause of it. I would say it's the setting of the images. I'm guessing you've already loaded the images into memory. So perhaps the images are still fairly large and the ImageView has some sort of scaling attribute defined in XML? Otherwise, I'd say the slowdown is being caused elsewhere in your code.