【问题标题】:How to set custom ListAdapter to list view in appwidget?如何将自定义 ListAdapter 设置为 appwidget 中的列表视图?
【发布时间】:2013-01-24 23:29:42
【问题描述】:

我有一个列表视图。我需要为每个列表项创建一个自定义视图,因此我创建了一个自定义 ListAdapter,它提供了视图,其布局如下所示。但是如何使用 RemoteViews 将此 listAdapter 设置为小部件中的 ListView ?

<?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>

【问题讨论】:

  • 你想要这个列表视图的列表适配器还是远程视图?你也试过什么?
  • 我想将 ListView 添加到具有自定义视图项的小部件中。这是我的基本要求,所以我创建了一个 listAdapter 但似乎你不能使用普通的 listAdapter 并添加到小部件中的 ListView .. 以上布局是针对列表中的单个视图项...

标签: android android-listview android-adapter remoteview


【解决方案1】:

对于RemoteViews,您需要实现RemoteViewsFactory,而不是常见的Adapter。要返回自定义的RemoteView(是的,每个项目都是RemoteView,而不是View),您需要覆盖其getViewAt(int position) 方法。

此外,对于RemoteViews,您需要提供一个setAdapter() 而不是setAdapter(),它将返回RemoteViewsFactory 之上的客户。

最后,向将要显示RemoteViews 的客户端传递此服务的Intent

您需要在清单文件中声明的服务及其意图。这将使您的应用程序成为远程列表的提供者,例如其他应用程序或任何远程进程的视图。

【讨论】:

  • 您是否建议类似:remoteViewsServiceIntent = new Intent( context , MyRemoteViewsService.class);
  • 如何将自定义数据(我的 API 项目列表)传递给这个适配器?
猜你喜欢
  • 2011-10-20
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多