gpt4 book ai didi

Android - 通知内容 View 中的ListView

转载 作者:太空狗 更新时间:2023-10-29 13:09:48 27 4
gpt4 key购买 nike

我可以将 ListView 放在通知的内容 View 中吗?如果可以,我该怎么做?

在另一个应用程序中,我在小部件中有一个 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());
}
}

list 服务声明:

<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,但列表仍为空...

最佳答案

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

void setRemoteAdapter (int viewId, Intent Intent )

相当于调用setRemoteViewsAdapter(Intent)。 只能用于 App Widget。

所以我想我的问题“我可以将 ListView 放在通知的内容 View 中吗”的答案是...

关于Android - 通知内容 View 中的ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42484365/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com