RecyclerView.getChild(index) shows null when list is scrolled (index gets messed up)

后端 未结 3 1551
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 06:34

I\'ve been using SwipeableRecyclerView for my android application for enabling swipes for my recyclerView. RecyclerView contains a list of cardViews.

I was trying to

3条回答
  •  孤城傲影
    2020-12-31 07:15

    Since I don't have enough reputation points to comment on @yigit's answer, I thought I would give a way to retrieve all viewHolders from a RecyclerView regardless of what is currently in the viewGroup. Even with findViewHolderForAdapterPosition and ForLayoutPosition we still can get a null instance of the ViewHolder in the RecyclerView.

    To circumvent this, we can use findViewHolderForItemId[1] and pass the ID of the viewHolder in the adapter using RecyclerView.Adapter#getItemId(int position)[2]

    for (int i = 0; i < mAdapter.getItemCount(); i++) {
        RecyclerView.ViewHolder holder = 
                mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(i));
    }
    
    1. https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#findViewHolderForItemId(int)
    2. https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemId(int)

提交回复
热议问题