Android Recyclerview Findviewholderforadapterposition Returns Null

Android recyclerView findViewHolderForAdapterPosition returns null

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.

RecyclerView findViewHolderForAdapterPosition returns sometimes null

Finally found a solution that works for me. (this helped https://www.creospiders.com/2016/04/how-to-access-each-view-of-item-by.html , better than a post delay)

Setted the full screen layout on GONE at the beginning of the method so I can have no other view over the reycler, used getViewTreeObserver().addOnGlobalLayoutListener on recycler list to wait for the views to build and setted a boolean value that allows or not to cycle my view in onBindViewHolder (in case the video layout is at limit of being out of screen)

            nVideoFullScreenLayout.setVisibility(View.GONE);
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(uiOptions);
mHeader.setVisibility(View.VISIBLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

mVideoFullScreen = false;
mAdapter.setCyclingVideoItem(false);

final LinearLayout[] mAdapterRow = {null};

mList.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mList.getViewTreeObserver().removeOnGlobalLayoutListener(this);
mAdapterRow[0] = (LinearLayout) ((ViewHolder_Article_Video) mList.findViewHolderForAdapterPosition(mVideoPosition)).itemView;

setAndCleanLayouts(mAdapterRow[0]);
mAdapter.setCyclingVideoItem(true);
}
});

RecyclerView findViewHolderForLayoutPosition and findViewHolderForAdapterPosition returns null

So I found the solution! And the explanation.
recyclerView.childCount = Number of element in parent which is obviously different from the position!

    for (i in 0 until recyclerView.childCount) {
val view = recyclerView.getChildAt(i)
val viewHolder = recyclerView.findContainingViewHolder(view)
... // the purpose, getting the data from the user here
}

RecyclerView scroll makes findViewHolderForAdapterPosition return null

As Sevastyan has written in the comment, the RecyclerView immediately recycles the view as soon as the item is out of the screen. So if we call findViewHolderForAdapterPosition() for a view which is outside the screen we get a null value. (I am not confirming this is the actual case. But, this is what it seems to me.)

So I created a class that stores all the data about an item in the RecyclerView and stored all the colours and value of that item in the class. And when we are populating the view, set the all the colours based on data stored in that class.

PS: I THANK Sevastyan for not giving me the answer directly. But, only giving me the reason for getting that Exception.



Related Topics



Leave a reply



Submit