How to know when data is loaded inside custom RecyclerView?

丶灬走出姿态 提交于 2019-12-10 17:35:07

问题


I am create and using custom RecyclerView in my application. I need to call getChildAt(0).getWidth() before any working to save first item width in class field. For that I need to catch when data loading in adapter (otherwise getChildAt(0) will returns null).

How to catch data loading event inside custom RecyclerView?


回答1:


RecyclerView is already calling registerAdapterDataObserver and it's unregister counterpart on the adapter the supplied adapter.

This observer have several callbacks to let the Recycler know when data is added, removed or changed. You should take advantage of those.

Also, the moment the adapter changes the data, it's not the same moment the views are created/layout on the screen. You might have to call getViewTreeObserver().addOnGlobalLayoutListener to receive a callback when the children have been layout on the screen.

Hope it helps, happy coding.



来源:https://stackoverflow.com/questions/32496681/how-to-know-when-data-is-loaded-inside-custom-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!