I am trying to get a count of RecyclerView children. I've tried this:
myRView.getChildCount();
And this:
LinearLayoutManager lm = ((LinearLayoutManager)myRViews.getLayoutManager());
lm.getChildCount();
I need to get a child View at certain position so that's why I need this. If I call getChildAt(index)
I never get any child View.
Both of these methods return 0. How else to get a child View from RecyclerView? My items appear correctly and everything is working fine. I am calling these methods after I create my adapter and set it to RecyclerView. Thank you for your help.
The function is called when the view is not ready that's why it returns zero, use post() method will help.
myRView.post(new Runnable() {
@Override
public void run() {
Log.show("myRView.post " + myRView.getChildCount());
View childView = myRView.getChildAt(0);
}
});
I assume you are calling those methods before layout is calculated.
When you set the adapter, the layout will happen in the next view tree traversal. You can consider adding a ViewTreeObserver.
You could use your adapter getItemCount() function.
myRView.getAdapter.getItemCount();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With