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


当前回答

看起来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>

而且它没有任何问题。

其他回答

尽管建议

永远不要把一个可滚动视图放在另一个可滚动视图中

这是一个很好的建议,但是如果你在回收器视图上设置了一个固定的高度,它应该可以正常工作。

如果你知道适配器项布局的高度,你就可以计算出RecyclerView的高度。

int viewHeight = adapterItemSize * adapterData.size();
recyclerView.getLayoutParams().height = viewHeight;

更新: 这个答案现在已经过时了,因为有像NestedScrollView和RecyclerView这样的小部件支持嵌套滚动。

永远不要把一个可滚动视图放在另一个可滚动视图中!

我建议你让你的主布局回收者视图,并把你的视图作为项目回收者视图。

看一下这个例子,它展示了如何在回收视图适配器中使用多个视图。 链接到示例

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

RecyclerView不回收

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

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

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

Android支持库23.2

我使用CustomLayoutManager禁用RecyclerView滚动。 也不要使用回收器视图作为WrapContent,使用它作为0dp, Weight=1

public class CustomLayoutManager extends LinearLayoutManager {
    private boolean isScrollEnabled;

    // orientation should be LinearLayoutManager.VERTICAL or HORIZONTAL
    public CustomLayoutManager(Context context, int orientation, boolean isScrollEnabled) {
        super(context, orientation, false);
        this.isScrollEnabled = isScrollEnabled;
    }

    @Override
    public boolean canScrollVertically() {
        //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
        return isScrollEnabled && super.canScrollVertically();
    }
}

在RecyclerView中使用CustomLayoutManager:

CustomLayoutManager mLayoutManager = new CustomLayoutManager(getBaseActivity(), CustomLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(mLayoutManager);
        ((DefaultItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false); 
        recyclerView.setAdapter(statsAdapter);

哎哟XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_main"
    android:fillViewport="false">


    <LinearLayout
        android:id="@+id/contParentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <edu.aku.family_hifazat.libraries.mpchart.charts.PieChart
                android:id="@+id/chart1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/x20dp"
                android:minHeight="@dimen/x300dp">

            </edu.aku.family_hifazat.libraries.mpchart.charts.PieChart>


        </FrameLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">


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


    </LinearLayout>


</ScrollView>