开发者

Inflating a ListView with different number of ImageViews in each row

I'm starting to work with the Android LayoutInflater and I need some help.

I have a ListActivity which uses a layout defined in the following XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</LinearLayout>

This ListView is filled with an ArrayAdapter which generates each row by inflating another XML like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent"
        >
        <TextView
            android:id="@+id/item_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textSize="20dip"
            android:textColor="#FFF"
        />
        <ImageView
            android:id="@+id/item_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="6dip"
            an开发者_如何学JAVAdroid:src="@drawable/example_icon"
        />
    </LinearLayout>
</LinearLayout>

This is working fine, but now I want to show a variable number of icons for each item (so, each row of the ListView could have a different number of ImageViews under the TextView).

Maybe I have to define another XML for the list of icons and somehow build the ListView from nested layouts? I don't know what's the better way to do this. Any suggestions?

Thank you!

Edit: This is an example of what im trying to do: http://tinypic.com/r/2s9yniw/7


You'll just have to include a number of images in the XML layout for each row, and set the visibility of these to GONE when you no longer need them (you could put an image in one layout XML file and then include this 3 times on your row layout file).

You might be able to do it by inflating the image layout XML file as many times as you need for each row then adding it to the row view (but this gives problems with recycling if you only added 2 images the first time and the next row needs 3); but from what I remember you cannot add inflated content to an Adapter row or you get an exception (not entirely sure, so give it a try if in doubt).


In a particular row you can place the image views below the text view of that row and when you do not need to show them you can hide it by setting the visibility to GONE.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜