两件事:
views.setScrollPosition(R.id.lvWidget, 3);
是正确的调用。在内部,setScrollPosition(int, int) 调用 RemoteViews#setInt(viewId, "smoothScrollToPosition", position);。
viewId : your ListView's id
"smoothScrollToPosition" : method name to invoke on the ListView
position : position to scroll to
所以,您正在调用正确的方法。
以下是对AppWidgetManager#partiallyUpdateAppWidget(int, RemoteViews) 方法的注释——取自AppWidgetManager.java 的源代码。我相信它回答了你的问题:... Use with {RemoteViews#showNext(int)}, {RemoteViews#showPrevious(int)},{RemoteViews#setScrollPosition(int, int)} and similar commands...
/**
* Perform an incremental update or command on the widget specified by appWidgetId.
*
* This update differs from {@link #updateAppWidget(int, RemoteViews)} in that the RemoteViews
* object which is passed is understood to be an incomplete representation of the widget, and
* hence is not cached by the AppWidgetService. Note that because these updates are not cached,
* any state that they modify that is not restored by restoreInstanceState will not persist in
* the case that the widgets are restored using the cached version in AppWidgetService.
*
* Use with {RemoteViews#showNext(int)}, {RemoteViews#showPrevious(int)},
* {RemoteViews#setScrollPosition(int, int)} and similar commands.
*
* <p>
* It is okay to call this method both inside an {@link #ACTION_APPWIDGET_UPDATE} broadcast,
* and outside of the handler.
* This method will only work when called from the uid that owns the AppWidget provider.
*
* <p>
* This method will be ignored if a widget has not received a full update via
* {@link #updateAppWidget(int[], RemoteViews)}.
*
* @param appWidgetId The AppWidget instance for which to set the RemoteViews.
* @param views The RemoteViews object containing the incremental update / command.
*/
public void partiallyUpdateAppWidget(int appWidgetId, RemoteViews views) {
partiallyUpdateAppWidget(new int[] { appWidgetId }, views);
}
如评论所示,在views.setScrollPosition(R.id.lvWidget, 3) 之后调用partiallyUpdateAppWidget(appWidgetId, views)。
该评论还警告您:This method will be ignored if a widget has not received a full update via {#updateAppWidget(int[], RemoteViews)}。这可能意味着以下调用:
views.setRemoteAdapter(R.id.lvWidget, svcIntent);
views.setScrollPosition(R.id.lvWidget, 3);
不应一次更新。我建议您将这些调用分成两个单独的更新:
第一:
views.setRemoteAdapter(R.id.lvWidget, svcIntent);
mAppWidgetManager.updateAppWidget(appWidgetIds, views);
第二:
views.setScrollPosition(R.id.lvWidget, 3);
mAppWidgetManager.partiallyUpdateAppWidget(appWidgetId, views);
请注意,第一个是完整更新,而第二个只是部分。