0

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;
Skepller
  • 117
  • 1
  • 2
  • 10
  • How to try to find the RecyclerView in HospitalFragment? – János Sicz-Mesziár May 18 '19 at 17:10
  • Your `RecyclerView` is not properly constraints .. Set constraints properly and it will work . [See this](https://stackoverflow.com/questions/37603751/set-width-to-match-constraints-in-constraintlayout). – ADM May 18 '19 at 17:16
  • @JánosSicz-Mesziár I added in the question how i set the adapter on HospitalFragment – Skepller May 18 '19 at 17:32
  • @ADM I've added constraints, but it doesn't seem to have changed anything – Skepller May 18 '19 at 17:33
  • Okay, it seems like my suspicion is confirmed. My tips your `view` reference actually is the root layout. Root layout was `RecyclerView` but after your change this is not true any more. But your code won't fail at run-time because the Java code is written safely via using `instanceof`. You have to find `RecyclerView` by `view.findviewById()`. – János Sicz-Mesziár May 18 '19 at 17:37
  • Can you add XML for the layout that RecyclerView is inflating? – Himanshu Rawat May 18 '19 at 17:42

1 Answers1

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;
  • I ended up modifying really close to this after your comment, was gonna ask you to add an aswer so i could accept it! Thanks a lot János! – Skepller May 18 '19 at 17:48