【问题标题】:How does RemoteViewsFactory handle ViewTypes in Android?RemoteViewsFactory 如何处理 Android 中的 ViewTypes?
【发布时间】:2018-11-04 13:29:45
【问题描述】:

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

我刚刚偶然发现了一个案例,我肯定只有 4 个视图类型,但不断收到显示错误(一个项目的“正在加载...”)和一个日志条目,我将返回比 @987654322 更多的视图类型@ 建议。所以我随机添加了+1,现在它可以工作了。

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

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

【问题讨论】:

    标签: android listview android-widget android-appwidget android-remoteview


    【解决方案1】:

    好的,这是问题的答案(取自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。让我在我的应用中进行一些研究,但回答了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 2017-01-12
      • 2012-11-04
      • 2012-10-22
      • 2014-02-07
      相关资源
      最近更新 更多