Nested Recycler View. MVVM with Data Binding.

Mobile Apps Development A-Z Guide.

Volodymyr Babenko
Pharos Production
3 min readFeb 11, 2019

--

Kotlin and Android.

Give us a message if you’re interested in Blockchain and FinTech software development or just say Hi at Pharos Production Inc.

Or follow us on Youtube to know more about Software Architecture, Distributed Systems, Blockchain, High-load Systems, Microservices, and Enterprise Design Patterns.

Pharos Production Youtube channel

In this article, I would like to show how to create nested lists using data binding.

Step 1.

First, you need to configure your build.gradle(Module:app):

  • activate data binding
  • add the following dependencies
apply plugin: 'kotlin-kapt'android{
dataBinding {
enabled = true
}
}
dependencies { implementation 'com.google.android.material:material:1.1.0-alpha03'
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
kapt 'com.android.databinding:compiler:3.2.1'
}

Step 2.

Create the following classes, which will be used as a data model. The first one is for the vertical scrolling cell and the second one is respectively horizontal. Third class— simple wrapper for NDO NestedDataObject:

data class Pdo(val value: Int) : HasType {

override fun getType(): Int {
return Holder.PARENT.type
}
}
class Ndo(val value: Int) : HasType {

override fun getType(): Int {
return Holder.NESTED.type;
}
}
class NestedDataObjectWrapper(val nestedDataObjectList: List<Ndo>) : HasType {

override fun getType(): Int {
return Holder.NESTED.type
}
}

Step 2.

Let's create a layout for holders in our RecyclerView:

item_parent_nolder.xml
item_nested_holder.xml

The built-in list should also have its horizontal layout

item_nested_horizontal_host.xml

Step 3.

Next, in the class of MainActivity we initiate a model and transmit directly into the screen layout.

MainActivity.kt

As you can now see, all the logic is placed on MainActivityViewModel.

Step 4.

Next, you need to create an adapter for recyclerViev — ParentAdapter. The difference from the classic adapter will be in the onCreateViewHolder function:

In the VievHolder class, using data binding, we directly transfer the data to the markup file for the main holder and the data from the newly created NestedAdapter for horizontal scroll.

Parent view holder.

--

--