Reputation: 817
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. 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. 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?
(Even if I change the background color of the RelativeLayout the text in the RecyclerView will be visible through it)
Here is the XML for this activity:
<?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>
Upvotes: 0
Views: 432
Reputation: 15423
Use NestedScrollView
to make whole view scrollable
<?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>
Upvotes: 1
Reputation:
Can I have the RelativeLayout not be transparent so that the RecyclerView items scroll "behind" it?
Just change the Constraint Layout to Linear Layout.
How would I be able to make the whole layout scroll together with the RecyclerView?
Logic: For that you would have to include the Relative layout in the XML file where you are inflating the RecyclerView content. 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.
That way you would have Relative Layout scroll with the RecyclerView content and inflated only once.
Upvotes: 1