更改布局后,RecyclerView消失

时间:2019-05-18 17:07:09

标签: android android-layout android-recyclerview

我有一个带有recyclerview的片段,它工作正常,只有recyclerview。

有效的原始XML :

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

我试图添加FAB,所以我添加了约束布局,并且我意识到,如果我在屏幕上添加任何内容,则recyclerview会停止显示项目。

使用以下代码,它将停止显示项目(空白片段):

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

我检查了类似的答案,但没有帮助。

这是我在HospitalFragment上设置适配器的方式

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

1 个答案:

答案 0 :(得分:1)

在我的评论之后,这里是代码片段:

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;