How to set custom ListAdapter to list view in appw

2020-04-10 23:21发布

I have a listView. I needed a custom view for each list item, so I've created a custom ListAdapter which gives the views, layout for which is given below. But how do I set this listAdapter to the ListView in the widget using RemoteViews ?

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

1条回答
小情绪 Triste *
2楼-- · 2020-04-10 23:47

Instead of the common Adapter, for RemoteViews you need to implement RemoteViewsFactory. To return custom RemoteView's (yes, RemoteView for each item and not View), you will need to override its getViewAt(int position) method.

Also, one just does not setAdapter() for RemoteViews, you would need to provide a RemoteViewsService which will return above RemoteViewsFactory to its clients.

Finally, to the client that is going to show the RemoteViews, you pass an Intent of this service.

The Service and its Intent you will need to declare in your manifest file. This will make your App a provider of remote List like views to other apps or any remote process.

查看更多
登录 后发表回答