我在 RemoteViewsService.RemoteViewsFactory 的 onDataSetChanged 函数中完成了这项工作。我不得不添加一个 AsynTask 来延迟滚动。如果 getLoadingView 返回的视图与 getViewAt 返回的所有视图高度相同,则可能不需要这样做。
@Override
public void onDataSetChanged() {
new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer[] params) {
int widgetId = params[0];
int count = params[1];
try {
Thread.sleep(800);
} catch (InterruptedException e) {
Log.d(TAG, "Scroll thread was interrupted");
}
String packageName = context.getPackageName();
RemoteViews rv = new RemoteViews(packageName, R.layout.widget_layout);
rv.setInt(R.id.list_view, "smoothScrollToPosition", count);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.partiallyUpdateAppWidget(widgetId, rv);
return null;
}
}.execute(this.appWidgetId, this.rowCount);
}