0

I am trying to put recyclerView with a screen (Fragment) that has textView.

The .xml file of the fragment i'm trying to add to:

<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=".ui.ways.WaysFragment">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/ways_quote"
        android:layout_marginTop="@dimen/card_margin"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:textSize="18sp"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        tools:listitem="@layout/custom_ways_item" />
</androidx.constraintlayout.widget.ConstraintLayout>

I will try to describe the appearance (result) of what I tried to do with code and words, rather than pictures, I think it will be easily understood that way.

At first I added textView and just put the recyclerView under it. And the result(appearance on my test device) I got look like:

Layout:
    ....
    TextView:
        ....

    ScrollView:
        ....
        and the contents of my recyclerView

By this I mean, the textView doesn't scroll but the recyclerView does.

Next I put the textView and recyclerView inside the a single scrollView. But the textView scrolls seperately from the recyclerView, as if textView and recyclerView were in different scrollView. the result(appearance on my test device) I got look like:

Layout:
    ....

    ScrollView:
        ....

        TextView:
            ....

    ScrollView:
        ....
        and the contents of my recyclerView

And my desired Appearances, is:

Layout:
    ....

    ScrollView:
    ....
    
        TextView:
            ....
        Contents of my recyclerView:
            ....
Edwin
  • 565
  • 11
  • 26
  • 1
    It’s hard to get scrolling to feel right with multiple scrollable views. As far as I know, the best way to achieve your desired result is to put the text view in your recycler view adapter as a different row item type. Kind of pain, I know. Now that there is ConcatAdapter, maybe it can be done more cleanly. – Tenfour04 Feb 08 '22 at 23:15
  • 1
    So, you want the _TextView_ to scroll as if it is part of the _RecyclerView_ so they all scroll together whether you touch the _TextView_ or the _RecyclerView_? – Cheticamp Feb 08 '22 at 23:16
  • @Cheticamp Yes that is what I want, Thanks for your comment. – Edwin Feb 08 '22 at 23:22
  • 1
    @Edwin Then make the _TextView_ its own type of view holder and place it into the _RecyclerView_ at the first position. If you don't know how to do this, I can post something about it or send you to a site that can. – Cheticamp Feb 08 '22 at 23:50
  • 1
    @Edwin Actually, [this](https://stackoverflow.com/a/26245463/6287910) may help. – Cheticamp Feb 08 '22 at 23:52

1 Answers1

0

I have discovered I named this question wrong it should have been "how to put header in a recyclerview using kotlin".

To do I created a new layout for the header, and an adapter.

Then I used ConcatAdapter To add the adapter of the header and recyclerview together.

I followed this tutorial on medium.

Edwin
  • 565
  • 11
  • 26