我试图实现一个布局,其中包含RecyclerView和ScrollView在相同的布局。

布局模板:

<RelativeLayout>
    <ScrollView android:id="@+id/myScrollView">

       <unrelated data>...</unrealated data>

       <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/my_recycler_view" />
    </ScrollView>   
</RelativeLayout>

问题:我可以滚动到ScrollView的最后一个元素。

我尝试过的事情:

在ScrollView里面的卡片视图(现在ScrollView包含RecyclerView) -可以看到卡片直到RecyclerView。 最初的想法是使用RecyclerView来实现这个ViewGroup而不是ScrollView,其中一个视图类型是CardView,但我得到了与ScrollView完全相同的结果。


当前回答

您可以尝试将回收器视图高度设置为wrap_content。 在我的情况下,它工作得很好。我尝试在滚动视图中使用2个不同的回收器视图

其他回答

看起来NestedScrollView确实解决了这个问题。

我使用这个布局进行了测试:

<android.support.v4.widget.NestedScrollView
    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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/dummy_text" />

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

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

    </LinearLayout>

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

而且它没有任何问题。

在ScrollViews中放入RecyclerViews很好,只要它们不是自己滚动。在这种情况下,将其设置为固定高度是有意义的。

正确的解决方案是在RecyclerView高度上使用wrap_content,然后实现一个可以正确处理包装的自定义LinearLayoutManager。

复制这个LinearLayoutManager到你的项目:link

然后包装RecyclerView:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

然后像这样设置:

RecyclerView list = (RecyclerView)findViewById(R.id.list);
list.setHasFixedSize(true);
list.setLayoutManager(new com.example.myapp.LinearLayoutManager(list.getContext()));
list.setAdapter(new MyViewAdapter(data));

编辑:这可能会导致滚动的复杂性,因为RecyclerView可以窃取ScrollView的触摸事件。我的解决方案是完全抛弃RecyclerView,使用LinearLayout,以编程方式膨胀子视图,并将它们添加到布局中。

我知道我迟到了,但即使谷歌已经在android.support.v7.widget.RecyclerView上进行了修复,问题仍然存在

我现在得到的问题是RecyclerView与layout_height=wrap_content不采取所有项目的高度问题内ScrollView,只发生在棉花糖和牛轧糖+ (API 23, 24, 25)版本。 (更新:将ScrollView替换为android.support.v4.widget。NestedScrollView适用于所有版本。我不知何故错过了测试已接受的解决方案。在我的github项目中添加了这个演示。)

在尝试了不同的方法之后,我找到了解决这个问题的方法。

以下是我的布局结构:

<ScrollView>
  <LinearLayout> (vertical - this is the only child of scrollview)
     <SomeViews>
     <RecyclerView> (layout_height=wrap_content)
     <SomeOtherViews>

解决方法是用RelativeLayout包装RecyclerView。不要问我是怎么找到这个变通办法的!!¯\_()_/¯

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

完整的示例可在GitHub项目- https://github.com/amardeshbd/android-recycler-view-wrap-content

下面是一个演示截图,展示了修复的操作:

试试这个。很晚的回答,但肯定对将来的任何人都有帮助。

将ScrollView更改为NestedScrollView: < android.support.v4.widget.NestedScrollView > < android.support.v7.widget.RecyclerView … ... /> < / android.support.v4.widget.NestedScrollView > 在你的UI代码中,为Recyclerview更新它: recyclerView.setNestedScrollingEnabled(假); recyclerView.setHasFixedSize(假);

如果你把RecyclerView放在NestedScrollView里面,并启用RecyclerView . setnestedscrollingenabled (false);,滚动将工作得很好。 然而,有一个问题

RecyclerView不回收

例如,你的RecyclerView(在NestedScrollView或ScrollView内)有100个条目。 当Activity启动时,将创建100个项目(同时调用100个项目的onCreateViewHolder和onBindViewHolder)。 例如,对于每个项目,你将加载一个大图像从API =>活动创建-> 100图像将加载。 它使启动活动缓慢和滞后。 可能的解决方案: -考虑使用多重类型的RecyclerView。

然而,如果在你的情况下,只有几个项目在RecyclerView和回收或不回收不会影响性能很多,你可以在ScrollView内使用RecyclerView简单