本文介绍了在onCreateViewHolder中调用父级的getWidth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在RecyclerView适配器的onCreateViewHolder中获取父级的宽度可以吗?父母的尺寸在那个状态下被测量了,所以当我在父母上调用getWidth()时我不会得到0?

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?

推荐答案

是的,父级实际上是RecyclerView.在调用onCreateViewHolder时,它的getWidth()和getMeasuredWidth()都为非零值.

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

这篇关于在onCreateViewHolder中调用父级的getWidth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 20:08