3

I have a collection based widget and I would like to change the initial scroll position.

I've tried following in my RemoteViewsFactory..

       views.setScrollPosition(R.id.widgetList, myDesiredPosition);
       AppWidgetManager.getInstance(mContext).partiallyUpdateAppWidget(mAppWidgetId, views);

but it unfortunately does not work..I also tried to set that right in my WidgetProvider, did not work though. So, please..is there a way how to change scroll position of a listview wrapped into remoteviews?

Cœur
  • 37,241
  • 25
  • 195
  • 267
simekadam
  • 7,334
  • 11
  • 56
  • 79
  • i got the same issue. not solved so far – bofredo Oct 17 '13 at 09:49
  • I am starting to think, that it is impossible..I tried sending a message back to the `WidgetProvider` from my `RemoteViewsFactory`, but the problem is I just cant persist the original instance of the Wrapping `RemoteViews` instance:/ My current "solution" is to load just item starting with the index I want to have as initial.. – simekadam Oct 17 '13 at 12:04
  • have you managed to catch the scroll-event? it is driving me crazy that onClickListener is a piece of cake and onScrollListener doesn't exist :/ – bofredo Oct 17 '13 at 12:39
  • I haven't tried to do that, but I think it's not possible to directly attach an event to the RemoteView. You can set the onClickPendingIntent, that is how you propably handle the click..Or you managed to somehow add and direct click listener to the widget? – simekadam Oct 17 '13 at 13:05
  • ĵust the onClickPendingIntent. but I can't find anything similar that could be used for scrolling. my boss, wants to run an application when a scroll is detected, works fine with onClick, but he insists on launching with a scroll/swipe :-/ – bofredo Oct 17 '13 at 13:12
  • RemoteViews does have an interresting method called apply, which returns the view instance itself..then you can do anything with it, but I am aware, that simply adding an eventListener to it won't work, cause it will get garbage collected as soon as the remoteviewsfactory finishes with updating the widget.. – simekadam Oct 17 '13 at 13:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39422/discussion-between-simekadam-and-bofredo) – simekadam Oct 17 '13 at 13:32

1 Answers1

4

I've done this in the onDataSetChanged function in my RemoteViewsService.RemoteViewsFactory. I had to add an AsynTask to delay the scrolling. This may not be necessary if your views returned by getLoadingView are the same height as all the views returned by 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);
}
Mike
  • 5,482
  • 2
  • 21
  • 25