Android - Widget with Scrollable Listview Manual Refresh

前端 未结 1 1041
长发绾君心
长发绾君心 2020-12-08 05:53

I googled and found the code to manipulate listview data using RemoteViewsService and RemoteViewsService.RemoteViewsFactory. as below

相关标签:
1条回答
  • 2020-12-08 06:53

    You can update the ListView by using notifyAppWidgetViewDataChanged() method of AppWidgetManager. You will have to get the instance of AppWidgetManager, get the AppWidgetIds and the call appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, viewId);

    Pseudo Code,

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int appWidgetIds[] = appWidgetManager.getAppWidgetIds(
                               new ComponentName(context, WidgetProvider.class));
    appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listview);
    

    When you call notifyAppWidgetViewDataChanged() then the onDataSetChanged() method of RemoteViewsFactory will be called which is serving as a Adapter for your ListView. You can do web-service stuff and other operations in onDataSetChanged(), get the response and add it to your data-set which maybe ArrayList or any such Collection.

    For further readings/reference you can check Keeping Collection Data Fresh part from the docs. Also you can check a demo example from my github.

    0 讨论(0)
提交回复
热议问题