简体   繁体   English

RecyclerView顶部透明TextView

[英]Transparent TextView on top of RecyclerView

I have this activity where I have a RecyclerView but above it I have a TextView, now when I scroll in the RecyclerView the RelativeLayout with the text stays at the top of the screen and doesn't scroll with it.我有这个活动,我有一个 RecyclerView,但在它上面我有一个 TextView,现在当我在 RecyclerView 中滚动时,RelativeLayout 与文本保持在屏幕顶部并且不会随之滚动。 Now my problem is that the RelativeLayout view is transparent so when I scroll in the RecyclerView the text in the RelativeLayout will lay on top of the list of text elements in the RecyclerView, in other words to simplify what I mean, I have "text on text" if that makes sense.现在我的问题是 RelativeLayout 视图是透明的,所以当我在 RecyclerView 中滚动时,RelativeLayout 中的文本将位于 RecyclerView 中文本元素列表的顶部,换句话说,为了简化我的意思,我有“文本文本”,如果这是有道理的。 How can I either have the RelativeLayout not be transparent so that the RecyclerView items scroll "behind" it and aren't visible "underneath" the TextView or how would I be able to make the whole layout scroll together with the RecyclerView?我怎样才能让 RelativeLayout 不透明,以便 RecyclerView 项目在它“后面”滚动并且在 TextView 的“下方”不可见,或者我如何才能使整个布局与 RecyclerView 一起滚动?

(Even if I change the background color of the RelativeLayout the text in the RecyclerView will be visible through it) (即使我更改了 RelativeLayout 的背景颜色,RecyclerView 中的文本也将通过它可见)

Here is the XML for this activity:这是此活动的 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>

Can I have the RelativeLayout not be transparent so that the RecyclerView items scroll "behind" it?我可以让 RelativeLayout 不透明,以便 RecyclerView 项目在“后面”滚动吗?

Just change the Constraint Layout to Linear Layout.只需将约束布局更改为线性布局。

How would I be able to make the whole layout scroll together with the RecyclerView?我怎样才能使整个布局与 RecyclerView 一起滚动?

Logic : For that you would have to include the Relative layout in the XML file where you are inflating the RecyclerView content.逻辑:为此,您必须在 XML 文件中包含相对布局,您将在其中膨胀 RecyclerView 内容。 Now that would inflate the Relative Layout every time right.现在,这将每次正确地膨胀相对布局。

So to overcome this problem, whatever you are passing in the RecyclerView constructor, take a flag/count, only inflate the Relative Layout when count is equal to zero/ set the flag and when the count is greater than 0 or if flag is set, then hide the Relative Layout.因此,为了克服这个问题,无论您在 RecyclerView 构造函数中传递什么,都取一个标志/计数,仅当计数等于零/设置标志以及计数大于 0 或设置标志时才膨胀相对布局,然后隐藏相对布局。

That way you would have Relative Layout scroll with the RecyclerView content and inflated only once.这样,您将使用 RecyclerView 内容滚动相对布局并且仅膨胀一次。

Use NestedScrollView to make whole view scrollable使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM