如何在 remoteview 的帮助下创建 Listview?
How to create Listview with help of remoteview?
我正在尝试在 remoteview
的帮助下在小部件中创建动态 listview
,因为我想要小部件中的这个 listview
应用程序图标。我想显示来自应用程序明智的所有应用程序的传入通知。我想创建常设通知列表,当用户从列表视图中单击应用程序图标时,将显示该特定通知。我正在使用 API 19
获取所有通知并且也成功了,但我不知道如何使用 Remoteview
和 drawables(Icons)
.[=17 在小部件中创建 Listview
=]
您是否搜索或尝试过其他在小部件中包含 ListView 的示例?
请查看 github 上可用的 Weather Widget demo。
代码:
public class MyWidgetProvider extends AppWidgetProvider {
private static HandlerThread sWorkerThread;
private static Handler sWorkerQueue;
public MyWidgetProvider() {
// Start the worker thread
sWorkerThread = new HandlerThread("MyWidgetProvider-worker");
sWorkerThread.start();
sWorkerQueue = new Handler(sWorkerThread.getLooper());
}
public void onUpdate(Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; ++i) {
...
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setRemoteAdapter(R.id.lvWidget, svcIntent);
sWorkerQueue.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
views.setScrollPosition(R.id.list, 3);
appWidgetManager.partiallyUpdateAppWidget(appWidgetIds[i], views);
}
}, 1000);
appWidgetManager.updateAppWidget(appWidgetIds[i], views);
...
}
}
}
其行为如下:
请检查 github 项目和上面的示例代码。
我已经尝试过@iDroid Explorer 的解决方案,来自他之前的一个答案。但是列表视图没有出现在边缘面板中(PS:我希望它在 galaxy note 边缘面板中实现)。
所以我尝试了以下代码:
private RemoteViews updateWidgetListView(Context arg0, int i) {
//which layout to show on widget
RemoteViews remoteViews = new RemoteViews(arg0.getPackageName(),
R.layout.activity_main_sub);
//RemoteViews Service needed to provide adapter for ListView
Log.d("fff", "updateWidget: "+ i);
Intent svcIntent = new Intent(arg0, MyService.class);
//passing app widget id to that RemoteViews Service
svcIntent.putExtra("Id", i);
Log.d("fff", "receiver:"+appnM);
svcIntent.putExtra("appname", appnM);
//setting a unique Uri to the intent
//don't know its purpose to me right now
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
//setting adapter to listview of the widget
remoteViews.setRemoteAdapter(i, R.id.list_icon,svcIntent);
// remoteViews.setRemoteAdapter(i, svcIntent);
arg0.startService(svcIntent);
//setting an empty view in case of no data
remoteViews.setEmptyView(R.id.list_icon, R.id.empty_view);
Log.d("fff","end of myReceiver");
return remoteViews;
}
并且在 myService class 中我已经在列表中填充了必需的项目。
我从一个名为 WidgetListView1 的示例中得到了帮助,它是我从随机搜索中找到的。
我正在尝试在 remoteview
的帮助下在小部件中创建动态 listview
,因为我想要小部件中的这个 listview
应用程序图标。我想显示来自应用程序明智的所有应用程序的传入通知。我想创建常设通知列表,当用户从列表视图中单击应用程序图标时,将显示该特定通知。我正在使用 API 19
获取所有通知并且也成功了,但我不知道如何使用 Remoteview
和 drawables(Icons)
.[=17 在小部件中创建 Listview
=]
您是否搜索或尝试过其他在小部件中包含 ListView 的示例? 请查看 github 上可用的 Weather Widget demo。
代码:
public class MyWidgetProvider extends AppWidgetProvider {
private static HandlerThread sWorkerThread;
private static Handler sWorkerQueue;
public MyWidgetProvider() {
// Start the worker thread
sWorkerThread = new HandlerThread("MyWidgetProvider-worker");
sWorkerThread.start();
sWorkerQueue = new Handler(sWorkerThread.getLooper());
}
public void onUpdate(Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; ++i) {
...
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setRemoteAdapter(R.id.lvWidget, svcIntent);
sWorkerQueue.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
views.setScrollPosition(R.id.list, 3);
appWidgetManager.partiallyUpdateAppWidget(appWidgetIds[i], views);
}
}, 1000);
appWidgetManager.updateAppWidget(appWidgetIds[i], views);
...
}
}
}
其行为如下:
请检查 github 项目和上面的示例代码。
我已经尝试过@iDroid Explorer 的解决方案,来自他之前的一个答案。但是列表视图没有出现在边缘面板中(PS:我希望它在 galaxy note 边缘面板中实现)。
所以我尝试了以下代码:
private RemoteViews updateWidgetListView(Context arg0, int i) {
//which layout to show on widget
RemoteViews remoteViews = new RemoteViews(arg0.getPackageName(),
R.layout.activity_main_sub);
//RemoteViews Service needed to provide adapter for ListView
Log.d("fff", "updateWidget: "+ i);
Intent svcIntent = new Intent(arg0, MyService.class);
//passing app widget id to that RemoteViews Service
svcIntent.putExtra("Id", i);
Log.d("fff", "receiver:"+appnM);
svcIntent.putExtra("appname", appnM);
//setting a unique Uri to the intent
//don't know its purpose to me right now
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
//setting adapter to listview of the widget
remoteViews.setRemoteAdapter(i, R.id.list_icon,svcIntent);
// remoteViews.setRemoteAdapter(i, svcIntent);
arg0.startService(svcIntent);
//setting an empty view in case of no data
remoteViews.setEmptyView(R.id.list_icon, R.id.empty_view);
Log.d("fff","end of myReceiver");
return remoteViews;
}
并且在 myService class 中我已经在列表中填充了必需的项目。
我从一个名为 WidgetListView1 的示例中得到了帮助,它是我从随机搜索中找到的。