使用多个子项的ListView [英] ListView using multiple Subitems

查看:166
本文介绍了使用多个子项的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,并再次约一个List这个时候的问题..一个ListView ......

Hello and again a question about a List this time.. a ListView......

我使用一个BaseAdapter膨胀的布局,其中包括一个RelativeLayout的(家长)和4 ImagesViews这是alignBaseline(水平)到第一个ImageView的。(这并不重要),它们都具有相同的来源,是imageArray。问题是,getView法仅适用于创建的每个列表项火灾/重用/滚动到view..but没有为列表项(我ImageViews)的孩子的。所以,当你早有预料:他怎么可以设置图片为ImageViews来自同一来源,如果只getView火的每一个的listItem,但不是每一个列表项的儿童?

I'm using a BaseAdapter to inflate a layout which includes a RelativeLayout(Parent) and 4 ImagesViews which are alignBaseline(horizontal) to the first ImageView.(It doesn't really matter) They all have the same source, which is an imageArray. The Problem is that the getView-Method only fires for every ListItem created/reused/scrolled into the view..but not for the childs of the ListItem(my ImageViews). So as you already expected:"How can he set the Images to the ImageViews from the same source if getView only fires for every listItem, but not for the childrens of every listitem??".

我在这里stucked:

I stucked here:

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView img1;
    ImageView img2;
    ImageView img3;
    ImageView img4;
    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv_abc;

    if(convertView == null) {
        convertView = inflater.inflate(R.layout.customadapter_view_layout, null);
        img1 = (ImageView) convertView.findViewById(R.id.img_normal_portrait_1);
        img2 = (ImageView) convertView.findViewById(R.id.img_normal_portrait_2);
        img3 = (ImageView) convertView.findViewById(R.id.img_normal_portrait_3);
        img4 = (ImageView) convertView.findViewById(R.id.img_normal_portrait_4);
        tv1 =(TextView) convertView.findViewById(R.id.tv_normal_portrait_1);
        tv2 =(TextView) convertView.findViewById(R.id.tv_normal_portrait_2);
        tv3 =(TextView) convertView.findViewById(R.id.tv_normal_portrait_3);
        tv4 =(TextView) convertView.findViewById(R.id.tv_normal_portrait_4);
        tv_abc = (TextView) convertView.findViewById(R.id.tv_normal_portrait_abc);
        convertView.setTag(new Holder(img1,img2,img3,img4,tv1, tv2, tv3, tv4,tv_abc));
    } else {
        Holder holder = (Holder) convertView.getTag();
        img1 = holder.img1;
        img2 = holder.img2;
        img3 = holder.img3;
        img4 = holder.img4;
        tv1 = holder.tv1;
        tv2 = holder.tv2;
        tv3 = holder.tv3;
        tv4 = holder.tv4;
        tv_abc = holder.tv_abc;

    }
        img1.setImageDrawable(scaledImage[position]);
        img2.setImageDrawable(scaledImage[position+1]);
        img3.setImageDrawable(scaledImage[position+2]);
        img4.setImageDrawable(scaledImage[position+3]);
        tv1.setText(labels[position]);
        tv2.setText(labels[position+1]);
        tv3.setText(labels[position+2]);
        tv4.setText(labels[position+3]);


    return convertView;
}

}

(请忽略textViews)
如你已经提及的,这是的极端缓慢,它甚至不工作。为什么?正如我前面提到的,(从getView)的位置计数每一个列表项而不是列表项的每一个孩子。此外,当我试图这样我才发现原来位置(getView)只计算ListItems的。后来我想,我怎么能是愚蠢的想法是,getView知道(或更好的想知道)对童车在我的布局?更我恨解决方案,如上面......他们是缓慢的,复杂的,往往会导致成IndexOutOfBoundException。

(Please ignore the textViews) As you already mentioned, this is extremly slow and it doesn't even work. Why? As i mentioned before, Position(from getView) Counts every ListItem but not every Child of the Listitem. Furthermore when i tried this i found out that Position(from getView) only Counts ListItems. And later i thought, how could i be that stupid thinking that the getView knows(or better wants to know) about the Childs in my layout ?? Even more i hate Solutions like that above...they are slow, complicated and often lead into IndexOutOfBoundException.

所以,请人有一个更好的解决方案或一个完全不同的方式得到这个工作。(其它适配器,等等)。我AP preciate每一个帮助。谢谢

So please, do someone have a much better solution or a totally different way getting this work.(Other Adapter, whatever). I appreciate every help. Thank you

推荐答案

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.

这篇关于使用多个子项的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆