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


当前回答

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

RecyclerView不回收

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

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

其他回答

我使用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>

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

Android支持库23.2

将这一行添加到您的RecyclerView xml视图中:

android:nestedScrollingEnabled="false"

和您的RecyclerView将顺利滚动灵活的高度。

希望能有所帮助。

对于那些仅仅为了设计目的而尝试这样做的人,请离开它。重新设计你的应用,只留下RecyclerView。这将是比任何硬代码更好的解决方案。

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

RecyclerView不回收

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

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