ListView Player for Android RemoteViews

Im trying to scroll a ListViewto a specific position in AppWidget.

However, it does nothing, I also tried the setPosition method , but it did not work.

There are also no errors or stack traces.

the code:

                    if (list.size() == 0) {
                        loadLayout(R.layout.rooster_widget_header);
                        views.addView(R.id.header_container,
                                views);
                    } else {
                        views.setRemoteAdapter(R.id.lvWidget, svcIntent);
                        views.setScrollPosition(R.id.lvWidget, 3);
                    }
+4
source share
4 answers

Problem : It ListViewhas no children until it displays. Therefore, the call setScrollPositionimmediately after installing adpater is not affected. Below is the code in AbsListView that performs this check:

final int childCount = getChildCount();
if (childCount == 0) {
    // Can't scroll without children.
    return;
}

. ViewTreeObserver.OnGlobalLayoutListener ListView, . partiallyUpdateAppWidget runnable . Android git hub.

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

. 5- .

Auto scrolling of ListView in app widget

+5

, , . :

getListView().setSelection(((moreServices-1)*20)+1);

:

yourListView.setSelection(3);

, ...

, , ... , , ,

?

0

, , , , . . , :

appWidgetManager.updateAppWidget(widgetId, remoteViews);

, AppWidgetManager

/**
 * Refresh the views on the widget
 * NOTE: Use only in case we dont have direct acces to AppWidgetManager
 */
private void invalidateWidget() {
    ComponentName widget = new ComponentName(this, YourWidgetProvider.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(widget, mRv);
}

, . , , . , .

0

:

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

- . setScrollPosition(int, int) RemoteViews#setInt(viewId, "smoothScrollToPosition", position);.

viewId                   :    your ListView 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);
}

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

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

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

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

Firstly:

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

Secondly:

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

Please note that the first is a full update, and the second is a partial update.

0
source

All Articles