2013-02-09 35 views
2

我有一個listView。我需要每個列表項目的自定義視圖,所以我創建了一個自定義的ListAdapter,它給出了視圖,下面給出了佈局。但是,如何使用RemoteViews將此ListAdapter設置到小部件中的ListView?如何設置自定義ListAdapter在appwidget中列出視圖?

<?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" 
    android:background="@android:color/background_light" 
    android:layout_margin="1sp" 
    android:padding="10sp" 
    > 

    <TextView 
     android:id="@+id/widgetInfo1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/listItemDummy" 
     android:textColor="@android:color/black" 
     /> 

    <TextView 
     android:id="@+id/widgetInfo2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/listItemDummy" 
     android:textColor="@android:color/black" 
     /> 

</LinearLayout> 
+0

你想要一個列表適配器爲這個列表視圖或你想要一個遠程視圖?你還試過了什麼? – lokoko 2013-02-09 06:06:27

+0

我想將ListView添加到具有自定義視圖項目的窗口小部件。這是我的基本要求,所以我創建了一個listAdapter,但它似乎不能使用正常的listAdapter並將其添加到ListView中的小部件中。以上佈局適用於列表中的單個視圖項目... – Amol 2013-02-09 07:09:32

回答

2

取而代之的常見的Adapter,爲RemoteViews需要實現RemoteViewsFactory。要返回自定義RemoteView(是的,RemoteView爲每個項目而不是View),您將需要覆蓋其getViewAt(int position)方法。

而且,一個少了點setAdapter()RemoteView S,你就需要提供一個RemoteViewsService將返回上述RemoteViewsFactory向它的客戶。

最後,對於要顯示RemoteView的客戶端,您將通過此服務的Intent

服務及其意圖,您將需要在清單文件中聲明。這將使您的應用程序成爲遠程列表的提供者,如其他應用程序或任何遠程進程的視圖。

+0

您是否建議如下所示: remoteViewsServiceIntent \t \t \t \t \t =新意圖( \t \t \t \t \t \t \t上下文 \t \t \t \t \t \t \t,MyRemoteViewsService.class); – Amol 2013-02-09 13:32:31

+0

@AmolKamble http://developer.android.com/guide/topics/appwidgets/index.html#collections – 2013-02-09 15:07:11

相關問題