【发布时间】: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