We can use extension functions to add new behaviors to a class that we may not even have access to. We can also add extension functions to view groups. One such view group is recycler view. Let's see how we can access views of a recycler view using extension functions.
I'll be using Android Studio 3 to write code. You can get started by creating a new project in Kotlin with a blank activity in Android Studio 3+, as we won't be using any code from other recipes. You also need an intermediate understanding of Android development.
Kotlin has some operators that we can use on a class. We will be overloading one of these operators to get views of our view group:
- We can access views of a view group by overloading the
get
operator like this:
operator fun ViewGroup.get(position: Int): View { return getChildAt(position) }
- Now, in order to get a view from the view group, we can use either of the following methods:
val...