Android RemoteViews ListView Scroll

前端 未结 4 1933
花落未央
花落未央 2021-01-01 06:01

Im trying to scroll a ListView to a particular position in an AppWidget.

However it does not do anything, i also tried the setPosit

4条回答
  •  猫巷女王i
    2021-01-01 06:40

    Two things:

    views.setScrollPosition(R.id.lvWidget, 3);
    

    is the correct call. Internally, setScrollPosition(int, int) calls RemoteViews#setInt(viewId, "smoothScrollToPosition", position);.

    viewId                   :    your ListView's id
    "smoothScrollToPosition" :    method name to invoke on the ListView
    position                 :    position to scroll to
    

    So, you are calling the correct method.

    Following is a comment for the method AppWidgetManager#partiallyUpdateAppWidget(int, RemoteViews) - taken from the source code of AppWidgetManager.java. I believe it answers your question: ... 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.
     *
     * 

    * 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. * *

    * 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); }

    As the comment indicates, call partiallyUpdateAppWidget(appWidgetId, views) after views.setScrollPosition(R.id.lvWidget, 3).

    The comment also warns you: This method will be ignored if a widget has not received a full update via {#updateAppWidget(int[], RemoteViews)}. This might mean that the following calls:

    views.setRemoteAdapter(R.id.lvWidget, svcIntent);
    views.setScrollPosition(R.id.lvWidget, 3);
    

    should not be made in one update. I suggest that you break these calls into two separate updates:

    First:

    views.setRemoteAdapter(R.id.lvWidget, svcIntent);
    mAppWidgetManager.updateAppWidget(appWidgetIds, views);
    

    Second:

    views.setScrollPosition(R.id.lvWidget, 3);
    mAppWidgetManager.partiallyUpdateAppWidget(appWidgetId, views);
    

    Note that the first one is a full update, while the second one is only partial.

提交回复
热议问题