Android 是否可以让“回收器视图”之外的项目与列表一起滚动?

Android 是否可以让“回收器视图”之外的项目与列表一起滚动?,android,scroll,android-recyclerview,Android,Scroll,Android Recyclerview,我有一个屏幕,所有项目都封装在RecyclerView中,除了标题。现在的问题是,RecyclerView设置为特定宽度,而标头设置为match\u parent。我希望能够滚动标题和屏幕上的其他项目(因此它看起来像是RecyclerView的一部分),而不改变宽度。这是可能的吗 我的XML代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/activity_funky_recycl

我有一个屏幕,所有项目都封装在
RecyclerView
中,除了标题。现在的问题是,
RecyclerView
设置为特定宽度,而标头设置为
match\u parent
。我希望能够滚动标题和屏幕上的其他项目(因此它看起来像是
RecyclerView
的一部分),而不改变宽度。这是可能的吗

我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/activity_funky_recycler_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some random header"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:textSize="24sp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"/>

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

</LinearLayout>

提前谢谢。

试试这个

<ScrollView
    android:id="@+id/activity_funky_recycler_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="5dp">

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some random header"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:textSize="24sp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"/>

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

</LinearLayout>
<android.support.v4.widget.NestedScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

        <android.support.v7.widget.CardView
            android:id="@+id/cardview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Info CardView1"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

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

            </LinearLayout>

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

        <android.support.v7.widget.CardView
            android:id="@+id/cardview2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/cardview1"
            android:layout_margin="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Info CardView2"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

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

            </LinearLayout>

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/cardview2"
            android:clipToPadding="false"
            android:paddingTop="0dp"/>

    </RelativeLayout>

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

考虑使用。它实现了您想要的功能。

Wow。真不敢相信事情竟那么简单。一个问题,这个
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
需要什么?我没有看到任何名为
layout\u behavior
<android.support.v4.widget.NestedScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

        <android.support.v7.widget.CardView
            android:id="@+id/cardview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Info CardView1"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

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

            </LinearLayout>

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

        <android.support.v7.widget.CardView
            android:id="@+id/cardview2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/cardview1"
            android:layout_margin="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Info CardView2"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

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

            </LinearLayout>

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/cardview2"
            android:clipToPadding="false"
            android:paddingTop="0dp"/>

    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(true); // true: with header
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);            
final MyLinearLayoutManager layoutManager = new MyLinearLayoutManager(this, 
LinearLayoutManager.VERTICAL, false, getScreenHeight(this)); // final CustomLinearLayoutManager layoutManager = new CustomLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(recyclerViewAdapter);  // recyclerView.setNestedScrollingEnabled(false); // Disables scrolling for RecyclerView, however, CustomLinearLayoutManager used instead of MyLinearLayoutManager