Java RecyclerView顶部的透明文本视图

Java RecyclerView顶部的透明文本视图,java,android,layout,android-recyclerview,scroll,Java,Android,Layout,Android Recyclerview,Scroll,我有一个活动,其中我有一个RecyclerView,但在它上面我有一个TextView,现在当我在RecyclerView中滚动时,带有文本的RelativeLayout会停留在屏幕顶部,而不会随之滚动。现在我的问题是RelativeLayout视图是透明的,所以当我在RecyclerView中滚动时,RelativeLayout中的文本将位于RecyclerView中文本元素列表的顶部,换句话说,为了简化我的意思,如果这有意义,我有“文本对文本”。我如何使RelativeLayout不透明,从

我有一个活动,其中我有一个RecyclerView,但在它上面我有一个TextView,现在当我在RecyclerView中滚动时,带有文本的RelativeLayout会停留在屏幕顶部,而不会随之滚动。现在我的问题是RelativeLayout视图是透明的,所以当我在RecyclerView中滚动时,RelativeLayout中的文本将位于RecyclerView中文本元素列表的顶部,换句话说,为了简化我的意思,如果这有意义,我有“文本对文本”。我如何使RelativeLayout不透明,从而使RecyclerView项在其“后面”滚动,并且在TextView的“下面”不可见,或者如何使整个布局与RecyclerView一起滚动

(即使我更改了RelativeLayout的背景色,RecyclerView中的文本也将通过它可见)

以下是此活动的XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity"
    android:background="@drawable/gradient_background">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@color/colorPrimary">

        <View
            android:id="@+id/lineView"
            android:layout_width="30dp"
            android:layout_height="0.5dp"
            android:background="@drawable/divider_line"
            android:layout_centerVertical="true"/>

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:layout_toEndOf="@id/lineView"
            android:layout_margin="8dp"/>

    </RelativeLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        app:layout_constraintTop_toBottomOf="@+id/relativeLayout"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我可以让相对人不透明吗 回收视图项目滚动“后面”吗

只需将约束布局更改为线性布局

我怎样才能使整个布局与 回收视图

逻辑:为此,您必须在XML文件中包含相对布局,在该文件中,您正在膨胀RecyclerView内容。现在,这将使相对布局每次都膨胀

因此,为了克服这个问题,无论您在传递什么RecyclerView构造函数,都需要一个标志/计数,只有当计数等于零时才膨胀相对布局/设置标志,当计数大于0或如果设置了标志时,才隐藏相对布局

这样,您就可以使用RecyclerView内容进行相对布局滚动,并且只膨胀一次

我可以让相对人不透明吗 回收视图项目滚动“后面”吗

只需将约束布局更改为线性布局

我怎样才能使整个布局与 回收视图

逻辑:为此,您必须在XML文件中包含相对布局,在该文件中,您正在膨胀RecyclerView内容。现在,这将使相对布局每次都膨胀

因此,为了克服这个问题,无论您在传递什么RecyclerView构造函数,都需要一个标志/计数,只有当计数等于零时才膨胀相对布局/设置标志,当计数大于0或如果设置了标志时,才隐藏相对布局


这样,您就可以使用RecyclerView内容进行相对布局滚动,并且只膨胀一次。

使用
NestedScrollView
使整个视图可滚动

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:fillViewport="true"
    tools:context=".Activity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/gradient_background">

        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary">

            <View
                android:id="@+id/lineView"
                android:layout_width="30dp"
                android:layout_height="0.5dp"
                android:background="@drawable/divider_line"
                android:layout_centerVertical="true"/>

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="24sp"
                android:layout_toEndOf="@id/lineView"
                android:layout_margin="8dp"/>

        </RelativeLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="16dp"/>

    </LinearLayout>
</androidx.core.widget.NestedScrollView>

使用
NestedScrollView
使整个视图可滚动

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:fillViewport="true"
    tools:context=".Activity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/gradient_background">

        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary">

            <View
                android:id="@+id/lineView"
                android:layout_width="30dp"
                android:layout_height="0.5dp"
                android:background="@drawable/divider_line"
                android:layout_centerVertical="true"/>

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="24sp"
                android:layout_toEndOf="@id/lineView"
                android:layout_margin="8dp"/>

        </RelativeLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="16dp"/>

    </LinearLayout>
</androidx.core.widget.NestedScrollView>


我刚刚注意到,当我现在滚动时,我的渐变背景也会“移动”,而之前它不会移动,你知道如何解决这个问题吗?另外,为什么嵌套scrollview上的布局高度是“wrap\u content”而不是“match\u parent”,只是想知道?通过将“android:background”从LinearLayout移动到嵌套scrollview来修复它!我刚刚注意到,当我现在滚动我的梯度背景“移动”,而在它不会移动之前,你知道如何解决这个问题吗?另外,为什么嵌套scrollview上的布局高度是“wrap\u content”而不是“match\u parent”,只是想知道?通过将“android:background”从LinearLayout移动到嵌套scrollview来修复它!