RemoteViewsFactory 如何处理 Android 中的 ViewType?

How does RemoteViewsFactory handle ViewTypes in Android?

由于 RemoteViewFactory 中没有 getItemViewType() 方法 - 它实际上是如何确定 ViewType 的?

我偶然发现了一个案例,我确实只有 4 个 viewType,但不断出现显示错误(一个项目 "loading...")和一个日志条目,我会 return 比getViewTypeCount 建议。所以我随机添加了+1,现在它起作用了。

所以在我看来,实际的 viewType 是由底层 ListAdapter/AppWidgetHost 猜测的,如果您对 RemoteViews 实例进行大量修改,它会将其检测为新的 viewType...

有谁知道实际发生了什么?

好的,这是问题的答案(摘自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");
    }

因此 Android 使用 LayoutId 作为 viewTypeReference。让我在我的应用程序中做一些研究,但回答了我的问题。