Android recyclerView findViewHolderForAdapterPosition returns null

后端 未结 6 1131
醉梦人生
醉梦人生 2020-11-27 07:08

I want to click an item in recyclerView programmatically, I found a way do that:

recyclerView.findViewHolderForAdapterPosition(0).itemView.performClick();
         


        
6条回答
  •  不知归路
    2020-11-27 08:02

    According to the official documentation:

    if notifyDataSetChanged() has been called but the new layout has not been calculated yet, this method will return null since the new positions of views are unknown until the layout is calculated.

    It's not safe to use findViewHolderForAdapterPosition().

    While you call this method after listView.swapAdapter(listadapter, false); you'll always get a null as result because notifyDataSetChanged() will be called.

提交回复
热议问题