Skepller
Skepller

Reputation: 144

RecyclerView disappears when the layout is changed

I've got a fragment with a recyclerview in it, it works fine, and only has the recyclerview.

Original XML that works:

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:name="pt.lusofona.helpnow.HospitalFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="LinearLayoutManager"
    tools:context=".HospitalFragment"
    tools:listitem="@layout/fragment_hospital" />

I was trying to add a FAB, so i added a Constrained Layout and i realized that if i add anything to the screen, the recyclerview stops showing items.

With the code below, it stops displaying items (blank fragment):

<android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.v7.widget.RecyclerView xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:name="pt.lusofona.helpnow.HospitalFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="LinearLayoutManager"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:context=".HospitalFragment"
    tools:listitem="@layout/fragment_hospital" />

</android.support.constraint.ConstraintLayout>

I've checked similar answers, but nothing helped.

This is how i set the adapter on HospitalFragment

    if (view instanceof RecyclerView) {
        Context context = view.getContext();
        RecyclerView recyclerView = (RecyclerView) view;            
        adapter = new MyHospitalRecyclerViewAdapter(DummyContent.ITEMS, mListener);
        recyclerView.setAdapter(adapter);
    }
    return view;

Upvotes: 0

Views: 660

Answers (1)

After my comment here is the code snippet:

RecyclerView recyclerview = view.findViewById(R.id.list);
if (recyclerview != null) {
    Context context = view.getContext();
    adapter = new MyHospitalRecyclerViewAdapter(DummyContent.ITEMS, mListener);
    recyclerview.setAdapter(adapter);
}
return view;

Upvotes: 1

Related Questions