我试图实现一个布局,其中包含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完全相同的结果。


当前回答

另一种解决这个问题的方法是在ScrollView中使用ConstraintLayout:

<ScrollView>
  <ConstraintLayout> (this is the only child of ScrollView)
    <...Some Views...>
    <RecyclerView> (layout_height=wrap_content)
    <...Some Other Views...>

但我仍然会坚持使用杨培勇提出的androidx.core.widget.NestedScrollView方法。

其他回答

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

我也有同样的问题。我试过了,而且成功了。我正在分享我的xml和java代码。希望这能帮助到一些人。

这是xml文件

<?xml version="1.0" encoding="utf-8"?>

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

        <ImageView
            android:id="@+id/iv_thumbnail"
            android:layout_width="match_parent"
            android:layout_height="200dp" />

        <TextView
            android:id="@+id/tv_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Description" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Buy" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Reviews" />
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rc_reviews"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

    </LinearLayout>
</NestedScrollView >

下面是相关的java代码。这招很管用。

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setNestedScrollingEnabled(false);

新的Android支持库23.2解决了这个问题,你现在可以将wrap_content设置为RecyclerView的高度并正确工作。

Android支持库23.2

你也可以重写LinearLayoutManager来让RecyclerView平滑滚动:

@Override
public boolean canScrollVertically(){
    return false;
}

抱歉迟到了,但似乎有另一种解决方案可以完美地解决你提到的情况。

如果在回收器视图中使用回收器视图,它似乎工作得非常好。我亲自尝试并使用过它,它似乎一点也不慢,一点也不突兀。现在我不确定这是否是一个好做法,但嵌套多个回收器视图,甚至嵌套滚动视图减慢。但这似乎工作得很好。请试一试。我确信嵌套在这方面是完全没问题的。