RecyclerView not calling onCreateViewHolder or onBindView

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.