0

我有一个显示项目列表的预 Android 4 小部件。每个项目都有一个色块,显示三种颜色中的一种颜色和文本,其颜色和样式由应用程序数据的属性决定。该列表实现为垂直 LinearLayout,但我想将其转换为 ListView 以使小部件在 ICS 中可滚动。在使用 ListView 时,我无法弄清楚如何将基础项目数据属性映射到项目远程视图。

(下面的代码已简化)。

项目数据类是

class ItemData {
   String text;
   int textColor;
   int patchColor;
}

小部件顶部布局是

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget_top_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >    
    <LinearLayout
        android:id="@+id/widget_item_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        // Items are added here at runtime by my widget provider class.
    </LinearLayout>    
</FrameLayout>

每个项目视图都使用这种布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >    
    <FrameLayout
        android:id="@+id/widget_item_color"
        // background color is set at runtime by the widget provider based
        // on patchColor member of the item data.
        android:layout_width="6dip"
        android:layout_height="fill_parent"
         >
    </FrameLayout>

    <TextView
        android:id="@+id/widget_item_text_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        // Actual color is set at runtime by the widget provider based on 
        // item data.
        android:textColor="@color/color_place_holder"
        android:background="@android:color/transparent"
        // May be set at runtime to false, based on user's prefernces.
        android:singleLine="true"
        android:scrollHorizontally="false"
        android:ellipsize="end"
        >
    </TextView>     
</LinearLayout>

我的问题是,如果我将顶部布局中的 LinearLayout 更改为 ListView,我在哪里插入将项目数据映射到项目视图属性的逻辑?任何进行类似映射的示例代码?

4

0 回答 0