代码之家  ›  专栏  ›  技术社区  ›  Coova

展开嵌套在ContrintLayout中的RecyclerView

  •  0
  • Coova  · 技术社区  · 8 年前

    我在片段中遇到了一个问题,在初始化适配器之前,我试图膨胀一个视图并验证它是一个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 回复  |  直到 8 年前