Inflate RecyclerView嵌套在ContraintLayout

时间:2018-05-21 14:11:16

标签: android android-recyclerview

我在我的片段中遇到了一个问题,我在尝试给视图充气并在初始化适配器之前验证它是RecyclerView。

问题是因为布局不再仅仅是RecyclerView组件了,我已将它包装在ConstraintLayout中并添加了FloatingActionButton。

如何使视图的特定部分膨胀,或者处理此问题的最佳方法是,由于(view instanceof RecyclerView)代码,适配器永远不会被初始化。

View view = inflater.inflate(R.layout.fragment_search_list, container, false);

Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;

if (view instanceof RecyclerView) {
    recyclerView.setLayoutManager(new LinearLayoutManager(context));

    mySearchRecyclerViewAdapter = new MySearchRecyclerViewAdapter(gameList, mListener);
    recyclerView.setAdapter(mySearchRecyclerViewAdapter);
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.scuvanov.weplay.fragment.SearchFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:name="com.scuvanov.weplay.fragment.SearchFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"
        app:layoutManager="LinearLayoutManager"
        tools:listitem="@layout/fragment_search">

    </android.support.v7.widget.RecyclerView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:layout_marginEnd="15dp"
        android:clickable="true"
        android:focusable="true"
        android:src="@drawable/ic_action_search"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案