208

My RecyclerView does not call onCreateViewHolder, onBindViewHolder even MenuViewHolder constructor, therefore nothing appears in RecyclerView. I put logs for debugging, and no log is shown. What might be the problem?

My adapter:

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MenuViewHolder> {
private LayoutInflater inflater;
List<Menu> data = Collections.emptyList();

public MenuAdapter(Context context, List<Menu> data) {
    Log.i("DEBUG", "Constructor");
    inflater = LayoutInflater.from(context);
    Log.i("DEBUG MENU - CONSTRUCTOR", inflater.toString());
    this.data = data;
    for(Menu menu: this.data){
        Log.i("DEBUG MENU - CONSTRUCTOR", menu.menu);
    }
}

@Override
public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.row_menu, parent, false);
    MenuViewHolder holder = new MenuViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MenuViewHolder holder, int position) {
    Log.i("DEBUG MENU", "onBindViewHolder");
    Menu current = data.get(position);
    holder.title.setText(current.menu);
}

@Override
public int getItemCount() {
    return 0;
}

class MenuViewHolder extends RecyclerView.ViewHolder {
    TextView title;
    ImageView icon;

    public MenuViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.menuText);
    }
}

My custom row XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/menuText"
    android:text="Dummy Text"
    android:layout_gravity="center_vertical"
    android:textColor="#222"/>

and my Fragment:

public NavigationFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mUserLearnedDrawer = Boolean.valueOf(readFromPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, "false"));
    if (savedInstanceState != null) {
        mFromSavedInstaceState = true;
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_navigation, container, false);
    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.drawer_list);
    MenuAdapter adapter = new MenuAdapter(getActivity(), getData());
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(adapter);
    return view;
}
RobC
  • 22,977
  • 20
  • 73
  • 80
Malinosqui
  • 2,222
  • 2
  • 13
  • 11
  • why u set recyclerView.setAdapter(adapter); 2 times? – Panayiotis Irakleous Jan 07 '15 at 00:24
  • Another cause of this problem is mistakenly making the item layout match the height of the parent, so only one item is shown at a time. – Joe Lapp May 23 '19 at 23:07
  • In my case help this : invoicesRecyclerView.setHasFixedSize(true) invoicesRecyclerView.setLayoutManager(GridLayoutManager(this, 1)) – Alexei Dec 19 '19 at 16:10
  • go to your xml. And change your layout to wrap content. The one element is hiding the next one, so the methods are not working! – Koala Apr 19 '19 at 03:06

37 Answers37

535

Another one is make sure you set layout manager to RecyclerView:

recycler.setLayoutManager(new LinearLayoutManager(this));
Kushal
  • 8,100
  • 9
  • 63
  • 82
Lay Leangsros
  • 9,156
  • 7
  • 34
  • 39
291

Your getItemCount method returns 0. So RecyclerView never tries to instantiate a view. Make it return something greater than 0.

for example

@Override
public int getItemCount() {
    return data.size();
}
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
46

i forgot to add below line after i adding it worked for me

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
saigopi.me
  • 14,011
  • 2
  • 83
  • 54
26

I really really hope this helps someone someday. I just spent over an hour on this one going nuts!

I had this:

projects_recycler_view.apply {
            this.layoutManager = linearLayoutManager
            this.adapter = adapter
        }

When I should have had this:

projects_recycler_view.apply {
            this.layoutManager = linearLayoutManager
            this.adapter = projectAdapter
        }

Because I foolishly called my adapter adapter, it was being shadowed by the recycler view's adapter in the apply block! I renamed the adapter to projectAdapter to differentiate it and problem solved!

the_new_mr
  • 3,476
  • 5
  • 42
  • 57
20

Please set layout manager to RecyclerView like below code:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view_right);
//set your recycler view adapter here
NavigationAdapter adapter = new NavigationAdapter(this, FragmentDrawer.getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Asghar Musani
  • 568
  • 4
  • 20
Mandeep Yadav
  • 707
  • 6
  • 17
18

This does not apply for your particular case. But this might help someone else.

This reason could be careless usage of the following method

recyclerView.setHasFixedSize(true);

If not used with caution, this might cause the onBindView and onCreateViewHolder to not be called at all, with no error in the logs.

venky
  • 2,141
  • 3
  • 16
  • 20
11

Maybe it helps someone but if you set your RecycleView's visibility to GONE the adapter methods won't be called at all... Took me some time to figure it out.

breakline
  • 5,776
  • 8
  • 45
  • 84
10

For what it's worth, I observed it when I set the recycler view adapter before the adapter was actually initialized. Solution was to make sure recyclerView.setAdapter(adapter) was called with a non-null adapter

kip2
  • 6,473
  • 4
  • 55
  • 72
6

Setting the height of the TextView in the custom.xml or RecyclerView. If height is wrap-content, the RecyclerView will not be visible.

AL.
  • 36,815
  • 10
  • 142
  • 281
jian lang
  • 61
  • 1
  • 2
  • Spent so much time on this.. the height of the recycler view was zero. When used in an activity, it somehow expanded itself. In a fragment inside other layouts.. it didn't – Asif Shiraz Jun 13 '20 at 22:11
4

This happened when my RecyclerView was inside a ScrollView and I was using Android Support Library 23.0. To fix, I updated to Android Support Library 23.2:

In build.gradle:

dependencies {
    ...
    compile 'com.android.support:appcompat-v7:23.2.+'
    compile 'com.android.support:cardview-v7:23.2.+'
}
J Wang
  • 2,075
  • 1
  • 20
  • 26
4

I had the same problem, because I was using android.support.constraint.ConstraintLayout in layout resource. Changing to FrameLayout helped.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Wild Teddy
  • 334
  • 8
  • 7
  • Thank you for your solution. Any idea why it behave like this if the View parent is `android.support.constraint.ConstraintLayout` ? – Adrian Buciuman Sep 24 '18 at 13:05
4

Add this to Your Adapter Class

 @Override
    public int getItemCount() {
        return mDataset.size();
    } 
JimHawkins
  • 4,843
  • 8
  • 35
  • 55
Brinda Rathod
  • 2,693
  • 1
  • 20
  • 32
4

You can add this in your XML widget RecyclerView

 app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

or manually

reyclerview.setLayoutManager(new LinearLayoutManager(ActivityName.this));
Reylar
  • 41
  • 2
3

My problem was layout_height and layout_width of the RecyclerView set to 0dp.

Zezi Reeds
  • 1,286
  • 1
  • 16
  • 29
3

For those having this problem in Kotlin, use this code instead:

recycler.layoutManager = LinearLayoutManager(context)

Note that you can use LinearLayoutManager even if your recyclerView is using constraintLayout.

Andrew Lee
  • 446
  • 4
  • 7
2

Other option is if you send this list objetc using get,verify if you have correct reference,for example

private list<objetc> listObjetc;
//Incorrect
public void setListObjetcs(list<objetc> listObjetc){
  listObjetc = listObjetc;
}

//Correct
public void setListObjetcs(list<objetc> listObjetc){
  this.listObjetc = listObjetc;
}
David Hackro
  • 3,652
  • 6
  • 41
  • 61
2

In my case, my list size was 0 and it was not calling the onCreateViewHolder method. I needed to display a message at center for the empty list as a placeholder so I did it like this and it worked like charm. Answer by @Konstantin Burov helped.

  @Override
public int getItemCount() {
    if (contactList.size() == 0) {
        return 1;
    } else {
        return contactList.size();
    }
}
MRX
  • 1,400
  • 3
  • 12
  • 32
2

I struggled with this issue for many hours. I was using a fragment. And the issue was not returning the view. Below is my code:

ProductGridBinding binding = DataBindingUtil.inflate( inflater, R.layout.product_grid, container, false);



    mLinearLayoutManager = new LinearLayoutManager(getActivity());
    mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    binding.rvNumbers.setHasFixedSize(true);
    binding.rvNumbers.setLayoutManager(mLinearLayoutManager);
    binding.rvNumbers.setAdapter(adapter);


    View view = binding.getRoot();


    return view;
TharakaNirmana
  • 10,237
  • 8
  • 50
  • 69
2

See my answer if you are using android data binding library - Make sure you are setting layout for recyclerview and item count must be greater than 0

 @BindingAdapter({"entries", "layout"})
    public static void setEntries(RecyclerView view, ArrayList<LoginResponse.User> listOfUsers, int layoutId) {
        if (view.getAdapter() == null) {
            view.setLayoutManager(new LinearLayoutManager(view.getContext()));
            SingleLayoutAdapter adapter = new SingleLayoutAdapter(layoutId) {
                @Override
                protected Object getObjForPosition(int position) {

                    return listOfUsers.get(position);
                }

                @Override
                public int getItemCount() {
                    return listOfUsers.size();
                }
            };
            view.setAdapter(adapter);
        }
    }

Happy coding :-)

Bajrang Hudda
  • 3,028
  • 1
  • 36
  • 63
2

If you put wrap_content as height of your list as well as the height of your item list, nothing will show up and none of your recyclerView functions will be called. Fix the height of the item or put match_parent for the list height. That solved my problem.

Meriam
  • 945
  • 8
  • 18
  • Additionally, putting "match_parent" for the item height can cause only the first item to show (because it's taking up the entire height of the RecyclerView). – Paul Jan 05 '21 at 04:26
2

You forget this line in the xml app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

or

    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
Pabel
  • 652
  • 7
  • 15
1

In my case I set the ID of my RecyclerView to "recyclerView". It seems that this ID was already defined somewhere, because when I changed that to different ID, the methods from the adapter were called properly.

1

If using a custom adapter do not forget to set the ItemCount to the size of your collection.

Dooie
  • 1,649
  • 7
  • 30
  • 47
1

My encouter was the onBindViewHolder( holder , position) overrided method of the RecyclerView.adaptor not being called. Method onCreateViewHolder was called, getItemCount was called. If you read the specs for Recyclerview Adapter, you will learn that if you have overrided onBindViewHolder( holder, position, List payloads) will be called in favour. So please be careful.

Yaojin
  • 341
  • 2
  • 6
1

In my case after changing Activity to use ViewModel and DataBinding, I had forgotten to remove setContentView method calling from onCreate method. By removing it my problem solved.

Siamak Ferdos
  • 3,181
  • 5
  • 29
  • 56
1

My mistake was inflating the wrong view in Fragment onCreateView.

Jeffrey
  • 1,998
  • 1
  • 25
  • 22
1

Here's my laundry list of reasons your recyclerView may not populate:

  • The Fragment/Activity didn't inflate correctly (failing to return the view in onCreateView or returning one other than the one the rest of code is using)
  • The RecyclerView's size has no height or width
  • The RecyclerView has no adapter
  • The RecyclerView has no layoutManager
  • Your text and background are the same color (i.e. it did populate but you're looking for a polar bear in snow storm)
  • There are no items (ie you don't have any data).
  • There are items, but the count returned is 0

I think that covers all the reasons I've seen a recyclerView not populate.

johngray1965
  • 163
  • 1
  • 9
0

Please do the following thing

@Override
public int getItemCount() {
    return data.size();
}
Saurav Mir
  • 124
  • 1
  • 11
0

For me, it was because the setHasStableIds(true); method was set. Remove that and it will work

Catalin
  • 109
  • 1
  • 2
  • But this method doesn't appear to be in the [mcve] in the question. If removing this _hint_ fixes something, it is probably an indication of a bug in the implementation of the view holder or maybe the notifier. –  Feb 04 '19 at 16:40
0

recyclerView.setAdapter(adapter); called The recyclerView list is populated by calling the method.

0

Make sure your XML and Koltin/Java code match. In my case, I had a problem with the ViewHolder since I had written:

var txt: AppCompatTextView = itemView.findViewById(R.id.txt)

But since my XML code is:

<TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        .../>

I had to change the privious row into:

var txt: TextView = itemView.findViewById(R.id.txt)

In addition, I also had to remove:

recyclerView.setHasFixedSize(true);

I hope this can help you :)

Mattia Ferigutti
  • 2,608
  • 1
  • 18
  • 22
0

In my case , I was missing calling notifyDataSetChanged() after setting list in adapter.

public void setUserList(List<UserList> userList) {
      this.userList = userList;
        notifyDataSetChanged();
}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
nkadu1
  • 223
  • 1
  • 3
  • 9
0

In my case I miss to set the orientation in my LinearLayout.

After adding orientation issue fixed.

 android:orientation="vertical"
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
0

I had an issue because i setup the layout by myself and have put

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/band_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</androidx.recyclerview.widget.RecyclerView>

and

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/band_recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:textAlignment="center">

resolved my issue

Boken
  • 4,825
  • 10
  • 32
  • 42
MykoCh
  • 11
  • 2
  • 5
0

Not the case in the main post, but it may come in handy for someone. If you set setHasStableIds(true);, override getItemId(position) is the adapter.

0

In my case I was returning null from onCreateView of fragment.

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding=DataBindingUtil.inflate(inflater,R.layout.fragment_add_budget,container,false)
        setRecyclerView()
        return null// that was the culprit
    }
Quraishi sazid
  • 91
  • 1
  • 12
0

My answer do not applies to yours but may apply to someones else cause I had the same problem you are facing.

So I actually did:

RecyclerView rv = new RecyclerView(this);

instead of

RecyclerView rv = findViewById(R.id.Recycler);

So make sure to take a note on that.

Exitron
  • 17
  • 12