RecyclerView wrap_content不适用于setHasFixedSize(true)

时间:2019-05-25 16:21:10

标签: android-recyclerview

我正在使用RecyclerView显示项目,但是当我以编程方式将其高度设置为wrap_contentsetHasFixedSize(true)时,这些项目不会显示。但是当我更改为setHasFixedSize(false)时,它可以正常工作。我希望RecyclerView用NestedScrollView和其他视图滚动(不希望RecyclerView滚动本身)。我该怎么办?

这是布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".Fragments.Fragment_Community"
    android:layout_height="match_parent"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="match_parent">

            <TextView android:layout_width="wrap_content"
                android:text="Header 1"
                android:textColor="@color/grey"
                android:textStyle="bold"
                android:layout_margin="10dp"
                android:textAllCaps="true"
                android:background="@color/white"
                android:textSize="@dimen/messages_text_size"
                android:gravity="center_vertical"
                android:layout_height="wrap_content"
                xmlns:android="http://schemas.android.com/apk/res/android" />




            <TextView android:layout_width="match_parent"
                android:text="Header 2"
                android:textColor="@color/grey"
                android:textStyle="bold"
                android:textAllCaps="true"
                android:background="@color/white"
                android:textSize="@dimen/messages_text_size"
                android:layout_margin="10dp"
                android:gravity="center_vertical"
                android:layout_height="wrap_content"

                xmlns:android="http://schemas.android.com/apk/res/android" />

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:scrollbars="none"
                android:background="@color/white"
                android:focusable="false"
                android:nestedScrollingEnabled="true"
                android:layout_height="wrap_content"
                android:id="@+id/rv_suggestions"/>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>


这是java代码:

      LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mSuggestions_Rv.setHasFixedSize(true);
        mSuggestions_Rv.setLayoutManager(linearLayoutManager);
        mSuggestions_Rv.setAdapter(mSuggestions_Adapter);

0 个答案:

没有答案
相关问题