简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM