【问题标题】:Android - ListView in notification content viewAndroid - 通知内容视图中的 ListView
【发布时间】:2020-03-31 00:33:29
【问题描述】:

我可以将 ListView 放在通知的内容视图中吗?如果是这样,我该怎么做?

在另一个应用程序中,我在一个小部件中有一个 ListView,使用 RemoteViews.setRemoteAdapter()、RemoteViewsService 和 RemoteViewsService.RemoteViewsFactory,它正在工作。

我尝试对通知执行相同的操作,但它不起作用。该应用程序没有崩溃,日志中没有错误,但没有显示 ListView。甚至没有调用 RemoteViewsService。

创建通知:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification);
views.setRemoteAdapter(R.id.lvNotificationList, new Intent(this, ListService.class));

builder
        .setSmallIcon(R.mipmap.ic_launcher)
        .setOngoing(true)
        .setContentTitle("Test")
        .setContent(views)
        .setCustomBigContentView(views)
        .setCustomHeadsUpContentView(views)
        .setCustomContentView(views);
Notification not = builder.build();

通知布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My list should appear below this" />

<ListView
    android:id="@+id/lvNotificationList"
    android:layout_width="match_parent"
    android:layout_height="100dp" />

列表服务:

public class ListService extends RemoteViewsService {
    private static final String TAG = "MyTag";

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        Log.d(TAG, "onGetViewFactory()");
        return new ListProvider(getApplicationContext());
    }
}

在 manifest 中声明 ListService:

<service
    android:name=".ListService"
    android:permission="android.permission.BIND_REMOTEVIEWS" />

列表提供者:

public class ListProvider implements RemoteViewsService.RemoteViewsFactory  {
    private static final String TAG = "MyTag";

    private Context context;

    public ListProvider(Context context) {
        this.context = context;
    }

    @Override
    public void onCreate() {
    }

    @Override
    public void onDestroy() {
    }

    @Override
    public int getCount() {
        return songs.size();
    }

    @Override
    public RemoteViews getViewAt(int position) {
        Log.d(TAG, "ListProvider.getViewAt(), position=" + position);

        RemoteViews ret = new RemoteViews(context.getPackageName(), R.layout.row_notification_list);
        ...
        return ret;
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public void onDataSetChanged() {
    }
}

ListView 上方的 TextView 已显示,但列表仍为空...

【问题讨论】:

  • 找到解决办法了吗?

标签: android listview notifications


【解决方案1】:

看起来我应该有 RTFM...来自https://developer.android.com/reference/android/widget/RemoteViews.html

void setRemoteAdapter (int viewId, Intent intent)

相当于调用 setRemoteViewsAdapter(Intent)。 只能用于应用小部件。

所以我猜我的问题“我可以将 ListView 放在通知的内容视图中”的答案是 ...

【讨论】:

  • 但这真的不可能吗?有什么想法吗?
【解决方案2】:

理论上可以在通知行中添加ListView,因为ListView 由@RemoteView 注释。见ListView源代码的第148行是here

*但是,在为ListView 充气后,它不能垂直滚动,因为该手势被通知抽屉捕获以滚动浏览所有其他通知集。因此,将ListView 添加到通知视图中只是理论上可行,但没有实际用途。

<ListView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/listView"
  android:layout_width="match_parent"
  android:layout_height="256dp"
  android:entries="@array/demo_string_array"
  android:background="@color/amber_dim"/>

在 res/values/string.xml 中添加一个字符串数组

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string-array name="demo_string_array">
    <item>Shuttle Badminton</item>
    <item>Tennis</item>
    <item>FootBall</item>
    <item>Basket Ball</item>
    <item>Table Tennis</item>
    <item>Chess</item>
    <item>Hockey</item>
</string-array>

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多