Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recyclerView.getChildCount() is returning different number of child everytime

I have created a RecyclerView that contains dynamic EditText fields. When the values in the EditText fields are edited I need to save them on a click of button. I am using recyclerView.getChildCount() to get the child count which is giving a different number everytime.

like image 837
Shivani Avatar asked Jul 01 '16 13:07

Shivani


People also ask

How many times Oncreateviewholder is called?

By default it have 5. you can increase as per your need. Save this answer.

Which RecyclerView method is called when getting the size of the data set?

getItemCount() : RecyclerView calls this method to get the size of the data set. For example, in an address book app, this might be the total number of addresses. RecyclerView uses this to determine when there are no more items that can be displayed.

How do you make a RecyclerView more performant?

If the size of ImageView in RecyclerView items is not fixed then RecyclerView will take some time to load and adjust the RecyclerView item size according to the size of Image. So to improve the performance of our RecyclerView we should keep the size of our ImageView to make it RecyclerView load faster.

Why is RecyclerView used select one?

Recycler View you could say is an efficient way to create list of views. If you have 1000 items like ur contact list , and If ur visible screen can show only 10 items at once, it will Create only 10+1 (or +2) Views and as u scroll , items/views that left will be reused (not create) to show new data.


2 Answers

Rather late to problem posted. Don't think the answers addressed the problem correctly. A few years since the problem of "getChildCount under reporting visible items displayed" was reported, yet my research found no one had fingered out what was causing this problem surfacing in some circumstances.

I had the same problem recently which led me to investigate further. Here's my discovery.

Say if 9 items on RecyclerView are visible. Call getChildCount(). It should return 9, maybe 1 or 2 less/more depending on partially visible items at the top and bottom. This will be the result most of the time ... until the soft keyboard shows for some TextEdit input. If you call getChildCount() about 500 msecs after the method call that led to showing the keyboard, the result will be less than 9. It will be the count of items unobstructed by the keyboard view. Weird, even though user didn't change the displayed contents of the RecyclerView. What's weird too is that even after the keyboard is dismissed, and all 9 items are again visible, calling getChildCount() will still return the incorrect under count! This happened with Android 11, and presumably with pre-11s (didn't test with post-11s).

A clue to solving this is the method RecyclerView.postInvalidateDelayed. If you really need to have the correct getChildCount number to work with, do something like this (after the keyboard is dismissed):

   myRecyclerView.invalidate();
   myRecyclerView.postDelayed(myRunnable, 200); 

Subtle problem!

Android folks at Google .. please note and address this bug.

like image 35
Ice Mann Avatar answered Sep 29 '22 08:09

Ice Mann


RecyclerView does what is indicated by its name. It recycles views. Say you have a list of 1000 entries but at any given time 3-4 of them are shown on the screen. RecyclerView's adapter always holds all of those children so calling recyclerView.getAdapter().getItemCount(); will return a 1000.

However RecyclerView only holds some of the items which are shown and are "close" to being shown so recyclerView.getChildCount() will return a smaller and not-constant value. For most applications you should then use the method provided by the adapter.

like image 194
sp0rk Avatar answered Sep 29 '22 09:09

sp0rk