androidlistviewwidgethome-screen-widget

Android Homescreen Widget ListView ViewHolder Pattern


I've got a homescreen widget loading a list of custom views fully up and running. Data is fed by a custom backend and new items are being properly reflected in the widget's list when I refresh it via a custom widget button.

One thing I noticed is that the homescreen widget is lacking in its support of widgets (views). One area I noticed was lacking was in respect to ListViews in that I was not able to implement the ViewHolder pattern. The main issue being that the getViewAt() method only provides the position as a parameter, unlike the regular ListAdapter, which also provides the convertView in its getView() method. An example of the RemoveViewFactory class is shown below:

class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
    private static final int mCount = 10;
    private List<WidgetItem> mWidgetItems = new ArrayList<WidgetItem>();
    private Context mContext;
    private int mAppWidgetId;

    public StackRemoteViewsFactory(Context context, Intent intent) {
        mContext = context;
        mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    ...
    public RemoteViews getViewAt(int position) {   
        // Construct a remote views item based on the app widget item XML file, 
        // and set the text based on the position.
        RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
        rv.setTextViewText(R.id.widget_item, mWidgetItems.get(position).text);

        ...
        // Return the remote views object.
        return rv;
    }
}

Is there a way to implement this pattern with homescreen widget and RemoteViews?


Solution

  • No, for the very simple reason that you do not have any views. The home screen has the views. The point behind a ViewHolder is to cache findViewById() lookups, and those happen in the home screen's process, not yours.