将RecyclerView嵌套在另一个布局中可隐藏列表

时间:2018-10-28 15:58:31

标签: android android-studio android-recyclerview

在Android Studio 3.2.1中,我创建了File-> New-> Fragment-> Fragment(列表),它自动创建了RecyclerView列表,该列表可以工作,但是如果我尝试将RecyclerView嵌套在另一个布局中,则会隐藏该列表。我已经尽一切努力使它正常工作,但是无法实现。我只想在屏幕底部列表下方创建一个操作栏。

由Android Studio自动生成:

browse_recycler_view.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="com.hssw.hssw_petmatch.FragmentBrowse"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        tools:context=".FragmentBrowse"
        tools:listitem="@layout/fragment_browse_item"/>

FragmentBrowse.java:

    public void onCreate(Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);
        if(getArguments() != null)
            {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
            }
        }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
        View view = inflater.inflate(R.layout.browse_recycler_view, container, false);
        // Set the adapter
        if(view instanceof RecyclerView)
            {
            Context context = view.getContext();
            RecyclerView recyclerView = (RecyclerView) view;
            if(mColumnCount <= 1)
                {
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
                }
            else
                {
                recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
                }
            recyclerView.setAdapter(new BrowseRecyclerViewAdapter(DummyContent.ITEMS, mListener));
            }
        return view;
        }

1 个答案:

答案 0 :(得分:1)

将RecyclerView放在协调器布局内。像下面一样

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<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="com.hssw.hssw_petmatch.FragmentBrowse"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
    tools:context=".FragmentBrowse"
    tools:listitem="@layout/fragment_browse_item"/>

</android.support.design.widget.CoordinatorLayout>