1

I have been given the task to implement a feature on Mobile application. Previously I had no experience with Android nor Java.

What I need is one screen (one fragment) to display two lists of events with uneven number of members one after another, let's call them ListX and ListY.

I've made layout containing two RecyclerViews and two labels (one label to act as header for each of recycler views). It is working as it is now and I have events displayed, but ListX is scrollable even though its layout_height is set to wrap_content (only 6 items displayed at the time) while ListY is not scrollable and is displaying all events in collection.

What I need is all items from ListX displayed in first RecyclerView (without scroll), and after that ListY displayed in second RecyclerView without scroll too.

Layout:

<FrameLayout
    android:id="@+id/resultsView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">


        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <TextView
                    android:id="@+id/label_daily" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/list_daily"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/label_daily"
                    tools:listitem="@layout/fragment_timetracking_event_item" />

                <TextView
                    android:id="@+id/label_unfinished" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/list_unfinished"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/label_unfinished"
                    tools:listitem="@layout/fragment_timetracking_event_item" />

            </LinearLayout>
        </ScrollView>

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

</FrameLayout>

As you can see from XML both RecyclerViews have layout_height set to wrap_content yet first RecyclerView is not obeying.

Here is sketch of how it should look like

Is there any way to put both lists in one RecyclerView and make custom separators? Or any other way to anchor beginning of one list to the end of the other and make them show all items without scroll?

I hope to get help here.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kruno MONO
  • 81
  • 13
  • How can you guarantee that all items in each recyclerview are visible? That is, if you want both lists to **not** scroll, what will the user do when the lists are larger than the available screen size? – Ben P. Jan 03 '18 at 18:29
  • I see there is a misunderstanding here. My problem is that RecyclerView has it's nested scroll and as I scroll down from one list to another Recycler is scrolling independantly. – Kruno MONO Jan 03 '18 at 18:32
  • I need whole ScrollView to scroll as a group without nested scrolls of RecyclerViews – Kruno MONO Jan 03 '18 at 18:34
  • You should check this. https://stackoverflow.com/questions/30531091/how-to-disable-recyclerview-scrolling – lib4backer Jan 03 '18 at 18:35
  • Items on lists are dynamic, meaning that sometimes there may be 5 items on ListX and 50 items on ListY or maybe 10 items on ListX and 0 on ListY – Kruno MONO Jan 03 '18 at 18:35

2 Answers2

1

You don't need 2 recyclerViews, you can achieve using single recylcerView. You should read about getItemViewType & using same in bindViewHolder & createViewHolder

You can specify these three types

  • Header
  • DailyItem
  • FinishItem

Create your list like this

  1. add Daily HEADER
  2. add Daily ITEMS
  3. add Unfinished HEADER
  4. add Unfinished ITEMS

Now you can render both lists in single recyclerview

Community
  • 1
  • 1
Akhil
  • 6,667
  • 4
  • 31
  • 61
  • Coud you please provide some sample code or working tutorial? I am really Java n00b and could not realize what you have stated above on my own, I've tried something like that today. – Kruno MONO Jan 03 '18 at 19:00
  • Should all these types extend RecyclerView.ViewHolder ? – Kruno MONO Jan 03 '18 at 19:37
  • It has worked, when I get some time I will post an answer to this question. Thank you very much for your help – Kruno MONO Jan 05 '18 at 11:34
  • You need to read https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Akhil Jan 05 '18 at 11:46
1

Your problem

...other way to anchor beginning of one list to the end of the other and make them show all items without scroll?

has a simple solution without rewriting your code to the one RecycleView - just put android.support.v4.widget.NestedScrollView instead of ScrollView in your xml layout. It seems to be a bug in the ScrollView widget, which is fixed in android.support.v4.widget.NestedScrollView.

P.S. After solving this problem you will face with other problem - Android RecycleView scrolls with no momentum, which is resolved in "RecyclerView within NestedScrollView Scrolling Issue". It is fine only for short item list in RecycleView because as it is mentioned in comments to above article

This is not a good solution since disabling nested scrolling will disable cell reuse as well, therefore loading all cells at once.

which will have impact on the memory consumption of your app in the case of long item list.

isabsent
  • 3,683
  • 3
  • 25
  • 46