Reputation: 15726
Android Studio 3.6
To show list on screen (Activity) I use androidx.recyclerview.widget.RecyclerView
and androidx.recyclerview.widget.RecyclerView.
Nice it's work fine.
But now I need to show two different lists (different type of items) in one screen. And scroll this both lists as one list. The elements of these two lists will be mixed up. Must be sorted by date. E.g.:
list1.item1_type1
list2.item1_type2
list2.item2_type2
list1.item2_type1
list1.item3_type1
list1.item4_type1
list1.item5_type1
list2.item3_type2
Is it possible to show TWO lists in one RecyclerView
?
Upvotes: 1
Views: 207
Reputation: 1775
Our problem is to sort out the lists based on date, but the problem is those are of different types. So we need to fix it by using another type/class that will have data of both class. Then we'll be able to sort our new list of combined type.
Let's recreate the problem first: Suppose we have two Types:
data class DataType1(var title: String, var date: Date)
data class DataType2(var title: String, var date: Date, var someOther: String)
val list1 = mutableListOf <DataType1> ()
val list2 = mutableListOf <DataType2> ()
list1.add(DataType1("Data 1", Date("01/01/2012")))
list1.add(DataType1("Data 2", Date("01/01/2013")))
list1.add(DataType1("Data 3", Date("01/01/2014")))
list2.add(DataType2("Data 21", Date("01/01/2022"), "Other"))
list2.add(DataType2("Data 22", Date("01/01/2010"), "Other"))
list2.add(DataType2("Data 23", Date("01/01/2005"), "Other"))
At this point we'll introduce new Type which is FixTypes
here.
class FixTypes(private val data1: DataType1?, private val data2: DataType2?) {
lateinit var date: Date
lateinit var title: String
var someOther: String? = null // those are not common field can be null
init {
if (data1 != null) {
date = this.data1.date
title = this.data1.title
} else if (data2 != null) {
date = this.data2.date
title = this.data2.title
someOther = this.data2.someOther
}
}
}
Then make a new list:
val fixedList = mutableListOf<FixTypes>()
list1.forEach {
val ft = FixTypes(it, null)
fixedList.add(ft)
}
list2.forEach {
val ft = FixTypes(null, it)
fixedList.add(ft)
}
As we've copied both of lists data to NewType we'll sort it:
fixedList.sortByDescending { it.date }
fixedList.forEach {
Log.d("TAG", it.date.toString())
}
Our sorted list is:
Sat Jan 01 00:00:00 GMT+06:00 2022
Wed Jan 01 00:00:00 GMT+06:00 2014
Tue Jan 01 00:00:00 GMT+06:00 2013
Sun Jan 01 00:00:00 GMT+06:00 2012
Fri Jan 01 00:00:00 GMT+06:00 2010
Sat Jan 01 00:00:00 GMT+06:00 2005
Now we'll modify out Adapter
s source list type to our newly created type.
That's all.
Oh another thing to mention, using two recycler
view won't work, because how will you mix up items?
Upvotes: 1
Reputation: 100
As Elias commented above, wrap two RecyclerViews in a NestedScrollView.
Setup and implement 2 different RecyclerViews. In the XML layout they are one on top of the other, but inside a a NestedScrollView. The effect will have both lists on top of each other, scrolling as one.
Upvotes: 1