如何在remoteview的帮助下创建Listview?

时间:2015-02-02 07:34:33

标签: android listview notifications remoteview

我想在listview的帮助下在widget中创建动态remoteview,因为我想在窗口小部件中使用listview个应用程序图标。我想显示来自所有应用程序明智的应用程序的传入通知。我想创建常设通知列表,当用户点击列表视图中的应用程序图标时,将显示该特定通知。我使用API 19获取所有通知并且也成功了,但我不知道如何在Listview以及Remoteview的小部件中创建drawables(Icons)

2 个答案:

答案 0 :(得分:5)

您是否在小部件中搜索或尝试其他具有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);
            ...
        }
    }
}

其行为如下:

enter image description here

请检查github项目和上面的示例代码。

答案 1 :(得分:1)

我已经从@iDroid Explorer的解决方案中尝试过他之前的一个答案。但是listview并没有出现在边缘面板中(PS:我希望这能在galaxy note edge的面板中实现)。

所以我尝试下面的代码:

    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类中,我已经在列表中填充了必需的项目。

我从一个名为WidgetListView1的例子中得到了帮助,我从随机搜索中找到了它。