RecyclerView LayoutManager findViewByPosition returns null

I am wondering what is the right and first possible moment to get the size of the first RecyclerView element?

I tried to use:

recyclerView.setLayoutManager(new GridLayoutManager(context, 2)); recyclerView.setAdapter(new MyDymmyGridRecyclerAdapter(context)); recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { recyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this); View firstRecyclerViewItem = recyclerView.getLayoutManager().findViewByPosition(0); // firstRecyclerViewItem is null here } }); 

but at that moment it returns null.

+7
android android-recyclerview
source share
3 answers

If you use OnGlobalLayoutListener , you must remember that onGlobalLayout can be called several times. Some of these calls may occur before the Layout is ready (and ready, I mean the moment when you can get the View dimensions by calling view.getHeight() or view.getWidth() ). So the correct way to implement your approach is:

 recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int width = recyclerView.getWidth(); int height = recyclerView.getHeight(); if (width > 0 && height > 0) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { recyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this); } } View firstRecyclerViewItem = recyclerView.getLayoutManager().findViewByPosition(0); } }); 

In addition, you should still be sure that during the call to findViewByPosition(0) :

  • Your RecyclerView's Adapter has at least one data item.
  • View at position 0 is currently displayed in RecyclerView

Tell me if this fixes your problem, if there is no other way to do what you need.

+3
source share

In my extended RecyclerView I override onChildAttachedToWindow , like this

 @Override public void onChildAttachedToWindow(View child) { super.onChildAttachedToWindow(child); if (!mIsChildHeightSet) { // only need height of one child as they are all the same height child.measure(0, 0); // do stuff with child.getMeasuredHeight() mIsChildHeightSet = true; } } 
0
source share

I had such a problem. I want to perform a default click in the first visible position of recylerview. I wrote the code for this on onResume, but that didn't work. I solved my problem by writing code in onWindowFocusChanged .

  @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if(isCalledForTheFirstTime) { LinearLayoutManager manager= (LinearLayoutManager) rcViewHeader.getLayoutManager(); int pos= manager.findFirstCompletelyVisibleItemPosition(); View view=manager.findViewByPosition(pos); if(view!=null) { view.performClick(); } // change the vaule so that it would not be call in case a pop up appear or disappear isCalledForTheFirstTime=false; } } 
0
source share

All Articles