【问题标题】:How to show one more item than FirestorePagingAdapter's list size at the beginning of a RecyclerView如何在 RecyclerView 的开头显示比 FirestorePagingAdapter 的列表大小多一个项目
【发布时间】:2019-04-20 16:20:01
【问题描述】:

我正在使用带有FirestorePagingAdapter 的recyclerview 来显示Firestore 集合中的一些文档。我需要显示一个额外的项目作为标题(数据不是来自适配器)。为简单起见,我们假设我只有一种视图类型和一个标题。所以我的getItemViewType() 方法看起来像这样。

@override
public int getItemViewType(int position) {
    if(position==0) return ViewType.HEADER;
    else return ViewType.BODY_ITEM;
}

onBindViewHolder()里面我正在这样做

@override
protected void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position, @NonNull final MyModel model) {
    if(holder instanceof HeaderHolder){
        holder.bind();
        return;
    }
    // because first item from adapter is shown at recyclerview's second position
    // based on the assumption that the parameter 'position' is recyclerview's position
    DocumentSnapshot mDocumentSnapshot = this.getCurrentList().get(position-1);
    holder.bindTo(mDocumentSnapshot);
}

所以我已经像这样覆盖了getItemCount() 方法

@override
public int getItemCount() {
    // because there is one extra header
    return super.getItemCount()+1;
}

但这样做会导致我的应用程序崩溃并记录以下错误消息

java.lang.IndexOutOfBoundsException: Item count is zero, getItem() call is invalid
        at android.arch.paging.AsyncPagedListDiffer.getItem(AsyncPagedListDiffer.java:194)
        at android.arch.paging.PagedListAdapter.getItem(PagedListAdapter.java:156)
        at com.firebase.ui.firestore.paging.FirestorePagingAdapter.onBindViewHolder(FirestorePagingAdapter.java:128)
        at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)

(只有前几行) 我的 Firestore 集合中有 12 个文档。 如果我不覆盖 getItemCount() 方法,那么 recyclerview 会显示 12 个项目,包括标题。最后一个文档由适配器获取,但不会由 recyclerview 显示。但是覆盖getItemCount() 方法会使应用程序崩溃(如上所述)。

我已阅读(至少尝试阅读:))AsyncPagedListDifferPagedListAdapterFirestorePagingAdapter.. 的源代码。 AsyncPagedListDiffergetItem()方法抛出异常java.lang.IndexOutOfBoundsException: Item count is zero, getItem() call is invalid

private PagedList<T> mPagedList;
private PagedList<T> mSnapshot;
.......................
.......................
.......................
public T getItem(int index) {
    if (mPagedList == null) {
        if (mSnapshot == null) {
            throw new IndexOutOfBoundsException(
                    "Item count is zero, getItem() call is invalid");
        } else {
            return mSnapshot.get(index);
        }
    }

    mPagedList.loadAround(index);
    return mPagedList.get(index);
    }

现在为什么会产生这个异常?我该如何解决这个问题?我将标头(再次显示的数据不是来自适配器)添加到回收站视图的方法有哪些替代方法。

也是提供给 `onBindViewHolder()` 的参数 'position' 实际上是适配器的位置或 recyclerviews 位置以添加新项目(文档称此适配器位置但我不确定) p>

【问题讨论】:

    标签: android android-recyclerview google-cloud-firestore


    【解决方案1】:

    您还必须覆盖超类 FirestorePagingAdapteronBindViewHolder 并在其中添加逻辑,如图所示。

        @override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
             if(position != 0){
                 super.onBindViewHolder(holder, position - 1)
             }
        }
    

    为回收站视图中的每个项目调用超类FirestorePagingAdapteronBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position)。当您向itemCount 添加+1 时,当recyclerview 到达最后一个位置时,它以IndexOutOfBoundsException 结束,因为数据库查询的结果以List 的形式存储在PagedStorage 类中,带有大小itemCount.

    这样,您的FirestorePagingAdapter 实现应该有2 个onBindViewHolder 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-23
      • 2019-02-22
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 2020-05-22
      相关资源
      最近更新 更多