android - getChildCount() 和 getChildAt() 为 RecyclerView 返回零

标签 android android-recyclerview

我正在尝试计算 RecyclerView 子项的数量。我试过这个:

myRView.getChildCount();

还有这个:

LinearLayoutManager lm = ((LinearLayoutManager)myRViews.getLayoutManager());
lm.getChildCount();

我需要在某个位置获得一个 subview ,所以这就是我需要这个的原因。如果我调用 getChildAt(index),我永远不会得到任何 subview 。

这两个方法都返回 0。还有什么方法可以从 RecyclerView 获取 subview ?我的元素显示正确,一切正常。我在创建适配器并将其设置为 RecyclerView 后调用这些方法。感谢您的帮助。

最佳答案

我假设您在计算布局之前调用这些方法。

当您设置适配器时,布局将在下一次遍历 View 树时发生。您可以考虑添加一个 ViewTreeObserver。

关于android - getChildCount() 和 getChildAt() 为 RecyclerView 返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28505202/

相关文章:

android - Checkstyle 自定义检查与 gradle (android studio)

android - Facebook "apprequests"过滤器不适用于 android

java - 如何将RecyclerView放入Fragment中?

android - RecyclerView 在设备方向更改时保存位置

android - 未调用 RecyclerView header 的 OnClickListener

android - 点击监听器应该在回收站 View 中的什么位置?

android - TableLayout 与 Recycler View ?

android - 使用 Dagger2 将适配器注入(inject) fragment

android - 在 Android Studio 中使用现有的 C++ 文件

android - Activity 未全屏显示