Calling Getwidth Of Parent In Oncreateviewholder
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?
Solution 1:
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 = 1080mLeft = 0mRight = 1080
Solution 2:
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
Post a Comment for "Calling Getwidth Of Parent In Oncreateviewholder"