Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 638
joe1806772 Avatar asked Mar 29 '26 00:03

joe1806772


1 Answers

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.

like image 196
joe1806772 Avatar answered Apr 02 '26 23:04

joe1806772



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!