Shivani
Shivani

Reputation: 121

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.

Upvotes: 10

Views: 12819

Answers (6)

Ice Mann
Ice Mann

Reputation: 11

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.

Upvotes: 1

saneesh
saneesh

Reputation: 169

I suppose you are using a LinearLayoutManager to show the list. It has a nice method called findViewByPosition that

Upvotes: 2

CleverChuk
CleverChuk

Reputation: 141

Hey if I understood your question correctly, You are trying to retrieve the value in the EditText on button click. if you want to retrieve from all visible view holders, do this

View v;
int itemCount = recyclerview.getChildCount();
for(int i = 0; i < itemCount; i++){
    v = recyclerview.getChildAt(i); /* assuming you have your EditText in a view group like LinearLayout*/
    EditText et = v.findViewById(R.id.my_text);
    String text = et.getText().toString()
    // todo process text
}

Upvotes: -1

buellas
buellas

Reputation: 331

There is a method called getItemCount() in LayoutManager, which returns exactly what you need. Basically it does what has been suggested in other answers.

Upvotes: 0

sp0rk
sp0rk

Reputation: 503

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.

Upvotes: 30

R. Zag&#243;rski
R. Zag&#243;rski

Reputation: 20268

By the children of RecyclerView you mean number of rows?

Then I would suggest calling adapter's method. Call:

recyclerView.getAdapter().getItemCount();

Upvotes: 7

Related Questions