RecyclerView not calling onCreateViewHolder or onB

2019-01-14 05:26发布

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);
    }
}

15条回答
不美不萌又怎样
2楼-- · 2019-01-14 05:37

I had also face the same issue where onCreateViewHolder or onBindView method was not getting invoke, only getItemCount was getting invoked. So it was basically issue with the height and weight of RecyclerView which was set to wrap_content or 0dp. After changing it to some value fixed the problem.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-14 05:39

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"/>

查看更多
Emotional °昔
4楼-- · 2019-01-14 05:39

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

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-14 05:41

I recently encountered this problem and apparently there are many possible causes. Try one or combination of these options, see which one(s) work(s) for you :)

1. Layout manager
Ensure that you set a layout manager to your recycler view as below

// create a layout manager
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(my_context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

// get recycler view
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_id);
// set
recyclerView.setLayoutManager(linearLayoutManager);

2. Fixed Size
Ensure that you state whether your recycler view has a fixed size or not

// set fixed size
recyclerView.setHasFixedSize(true); // or false depending on implementation

3. XML
Ensure that your recycler is not nested in too many layouts which may cause confusion such as combination of ScrollView and RelativeLayout. Make it simple, see if it fixes your issue

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
android:id="@+id/thread_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
查看更多
不美不萌又怎样
6楼-- · 2019-01-14 05:45

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.

查看更多
何必那么认真
7楼-- · 2019-01-14 05:45

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.

查看更多
登录 后发表回答