androidlistviewandroid-widgetandroid-appwidgetandroid-remoteview

How does RemoteViewsFactory handle ViewTypes in Android?


As there is no getItemViewType() method in RemoteViewFactory - how does it actually determine the ViewType?

I just stumbled upon a case where I DEFINITELY had only 4 viewTypes, but kept getting display errors ("loading..." for one item) and a log entry that I would return more viewTypes than the getViewTypeCount suggests. So I randomly added +1 and now it works.

So it seems to me that the actual viewType is guessed by the underlying ListAdapter/AppWidgetHost and if you do heavy modifications to a RemoteViews instance it will detect it as a new viewType...

Does anyone know what's actually happening?


Solution

  • OK, here's the answer to the question (taken from the Android sources of RemoteViewsListAdapter):

    for (RemoteViews rv: mRemoteViewsList) {
            if (!mViewTypes.contains(rv.getLayoutId())) {
                mViewTypes.add(rv.getLayoutId());
            }
        }
        if (mViewTypes.size() > mViewTypeCount || mViewTypeCount < 1) {
            throw new RuntimeException("Invalid view type count -- view type count must be >= 1" +
                    "and must be as large as the total number of distinct view types");
        }
    

    So Android uses the LayoutId as viewTypeReference. Leaves me with some research to do in my app, but answers my question.