1

Is it ok to get parent's width in onCreateViewHolder at RecyclerView adapter? Are parent's dimensions measured at that state so I don't get 0 when I call getWidth() on parent?

ferpar1988
  • 596
  • 2
  • 5
  • 17

2 Answers2

0

Yes, the parent is actually the RecyclerView. It has both a non-zero getWidth() and getMeasuredWidth() by the time onCreateViewHolder is called.

Easy way to verify this is set your debugger to stop in the method and inspect the parent.

parent = android.support.v7.widget.RecyclerView{d08d560 VFED..... ......ID 0,0-1080,1698 #7f0c0074 app:id/recyclerView1}

mMeasuredWidth = 1080
mLeft = 0
mRight = 1080
NameSpace
  • 10,009
  • 3
  • 39
  • 40
0

I came across a case where I actually got 0 on some occasions when calling parent.getWidth(), so I had to use getMeasuredWidth(). For anybody facing this issue or wondering about it, I suggest you read this answer

fhomovc
  • 301
  • 3
  • 15