RecyclerView not calling onCreateViewHolder or onBindView

61,507

Solution 1

This may also help someone

First Use

recyclerView.setAdapter(adapter);

And then:

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

So it will look like this:

recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

The order is reversed

Update:

Nowadays I simply use:

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

on the RecyclerView in the xml

Solution 2

Been chasing answer for over an hour.

Dont forget to call this one liner before setting your adapter.

recyclerView.setLayoutManager(new LinearLayoutManager(this));

It seems that recycler view do not have default layout manager option built in so we have to programatically add it.

Cheers if you found it helpful.

Solution 3

Make sure you recycle view is not child from nestedscrollview

for example, this code will not work.

<android.support.v4.widget.NestedScrollView
    android:id="@+id/responder_scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/darkGray"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            app:cardElevation="@dimen/spacing_medium"
            app:cardUseCompatPadding="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/responder_testimonial"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:text="Testimonial"
                    android:textSize="@dimen/general_font_size" />

                <TextView
                    android:id="@+id/responder_counter_testimonial"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/responder_testimonial"
                    android:text="170"
                    android:textSize="@dimen/general_font_size_small" />

                <Button
                    android:id="@+id/responder_btn_testimonial"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:text="Go" />

               <view
                    android:id="@+id/responder_list_testimonial"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/responder_testimonial"
                    class="android.support.v7.widget.RecyclerView"/>
            </RelativeLayout>
        </android.support.v7.widget.CardView>

    </FrameLayout>
</android.support.v4.widget.NestedScrollView>

then i use,

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<view
    android:id="@+id/responder_list_testimonial"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/responder_testimonial"
    class="android.support.v7.widget.RecyclerView"/>

Solution 4

I don't know if this will be helpful to anyone, but I almost had the same exact problem. My problem was not in either the fragment/class nor the recycler adapter. The problem was in parent XML layout where the actionbar took match_parent where the FrameLayout -in which the fragment is replaced- didn't get any available place to be shown. That's why the methods weren't called.

I guess similar scenarios might be the case for those facing the same problem. Double check every width and height in every XML file might be in the same tree, as the problem doesn't seem to be in the adapter at all.

Solution 5

My two pennies here. Just spent more than three hours debugging this.

Make sure that you have not used the following line carelessly.

recyclerView.setHasFixedSize(true);

Set the fixed size only when you are sure that the view would have a constant size. In case it is not, the onCreateViewHolder and onBindView methods might not get called at all.

Share:
61,507
Conti
Author by

Conti

Updated on July 05, 2022

Comments

  • Conti
    Conti almost 2 years

    Not getting any errors and all the data seems valid. For some reason, nether of the view related methods are being called. I have made sure of the following:

    • getItemCount() is the only adapter method being called and is returning a positive integer value, (I know this will be the area you guys will look at)

      • Constructor is being called, member variables are valid.

      • Parent View is a vertical LinearLayout; no scrollview, or any other view with their own scroll properties in sight.

      • containing fragment view is created and shown on screen.

    Here is the declaration in the fragment followed by the adapter. Any help would be appreciated as this has be completely baffled.

    SubMenuAdapter adapter = new SubMenuAdapter(getActivity(), mContentItems);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
    
    public class SubMenuAdapter extends RecyclerView.Adapter<SubMenuAdapter.ViewHolder> {
    private static final String TAG = String.format("==> %S", SubMenuAdapter.class.getSimpleName());
    
    private final List<ContentItem> mContentItems;
    private Context mContext;
    
    public SubMenuAdapter(Context context, List<ContentItem> contenItems) {
        Log.d(TAG, "Constructor called");
        mContentItems = contenItems;
        mContext = context;
    }
    
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d(TAG, "onCreateViewHolder called");
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_resource_efficiency, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder called");
    
        ContentItem item = mContentItems.get(position);
        holder.textName.setText(item.getName());
        FontSetter.setMyriadProRegular(mContext, holder.textName);
        Picasso.with(mContext).load("file://" + item.getPreviewImageDefault()).into(holder.imageIcon);
    }
    
    @Override
    public int getItemCount() {
        Log.d(mContext, String.format("getItemCount: %d", mContentItems.size()));
        return mContentItems.size();
    }
    
    // ViewHolder
    public static class ViewHolder extends RecyclerView.ViewHolder {
    
        TextView textName;
        ImageView imageIcon;
    
        public ViewHolder(View view) {
            super(view);
            textName = (TextView) view.findViewById(R.id.tv_resource_efficiency_option);
            imageIcon = (ImageView) view.findViewById(R.id.iv_resource_efficiency_icon);
        }
    }
    
  • IgorGanapolsky
    IgorGanapolsky over 8 years
    Indeed this problem was caused for me by nesting my RecyclerView in a ScrollView. To solve it, I set a custom LayoutManager on my RecyclerView.
  • raditya gumay
    raditya gumay over 8 years
    Hi, yes your right, i also use Custom LayoutManager
  • nikhil
    nikhil about 8 years
    You saved me!! was doing exactly the same thing
  • Abdul-Rahman Ahmad
    Abdul-Rahman Ahmad about 8 years
    Glad my answer helped
  • startoftext
    startoftext about 8 years
    I spent forever on this. I ran into this problem even though my recycler view was not a direct child of scrollview or nested scrollview. Is this a known bug or just by design?
  • raditya gumay
    raditya gumay about 8 years
    i dont know exactly. but if you want to use nestedscrollview, you also should use a view inside it.
  • igoris
    igoris over 7 years
    do you have any link to an explanation why this has to be this way?
  • Aerim
    Aerim over 7 years
    Hi @igoris sadly I do not, I only got to this solution by trial and error.
  • venky
    venky over 7 years
    Thanks a lot. This worked for me. Also, mentioned the same answer here
  • Gabriel
    Gabriel almost 7 years
    I've been looking for everywhere. And the problem was exactly that!
  • Vassily
    Vassily almost 7 years
    Contrarwise, replaced ScrollView with NestedScrollView and code starts to work (ScrollView > LinearLayout > RecyclerView). Anyway, with your help
  • raditya gumay
    raditya gumay almost 7 years
    Yes, you can do that.
  • CoolMind
    CoolMind about 6 years
    Thanks! Wasted many hours on it. onBindViewHolder sometimes didn't call. In my case I wrongly added setHasStableIds(true); in adapter's constructor.
  • krishnamurthy
    krishnamurthy about 6 years
    yes i forgot this , and tried whole day, finally ur answer helped me to cross check.
  • ralphgabb
    ralphgabb about 6 years
    know that feeling, and solved it in just a one line piece of code. Glad it helps.
  • daneejela
    daneejela over 5 years
    LayoutManager is one of the most annoying bugs that I've made millions of times and I always forgot about it. I'm going to print and frame on the wall: "Setup the Layout Manager!" :)
  • Sankalp
    Sankalp about 5 years
    Using Firestore: 1st point works for me. wrap_content, how can I forget that :-), Kudos for the added note. Thanks for the help.
  • Prashant Paliwal
    Prashant Paliwal about 5 years
    Always happy to help :)
  • Aerim
    Aerim about 5 years
    I would think that if you set the LayoutManager before the adapter it will already have measured something and it doesn't think it needs to do it again. This is just theory nothing proved
  • n_r
    n_r almost 5 years
    This was it! Thank you so much!
  • who-aditya-nawandar
    who-aditya-nawandar almost 3 years
    Genius! But it was combination of your answer + removing @VenakteshShukla's answer
  • Nadim Ansari
    Nadim Ansari over 2 years
    Thanks man !! Solved my issue.