This is the third in a series of articles which cover the fundamentals of creating and using RecyclerView
. If you already have a solid understanding of how to create a RecyclerView
, then carry on. Otherwise, consider starting with this post.
When displaying a list of data with RecyclerView
, you may want to have a response when an item is clicked. This response could open a new page with more data, present a toast, remove an item, etc. The possibilities are endless but they are all done using onClick()
.
Before creating the listener, create a function in the Activity
class that performs the action to be done upon click. …
This is the second in a series of articles which cover the fundamentals of creating and using RecyclerView
. If you already have a solid understanding of how to create a RecyclerView
, then carry on. Otherwise, consider starting with this post.
RecyclerView
is a great way to display a list of data items efficiently. For displaying a list of static data, the default adapter works great. However, in most use cases, RecyclerView
data is dynamic. Take a todo list app for example: new items are added, and completed items are removed. notifyItemInserted()
can insert new tasks at a specified index, but the issue comes when removing items. notifyItemRemoved()
is only useful if you have the position of the task you want to remove. It is possible to write code to get the position of the task to be removed and then call notifyItemRemoved()
, but this code can get messy. Calling notifyDataSetChanged()
is an option, but it redraws the entire view, even the unchanged parts, which is an expensive operation. …
Have you ever used an API and wanted to add functionality or a property to it?
You could inherit from the class or create a function that takes in an instance of the class to solve this problem. The Java programming language usually solves this problem with a Utils class but this does not show up in autocomplete which makes it harder to find and less intuitive to use. Both of these work as solutions but neither promote easy, readable code.
Thankfully, Kotlin comes to the rescue with extension functions and properties. These let you add functionality to a class without the need to inherit or create a function that takes in the class. Unlike the Java programming language’s equivalent, extensions appear in Android Studio’s autocomplete. …
RecyclerView
is a powerful UI widget that allows you to display a list of data in a flexible manner. When I was learning about RecyclerView
, I found there were a lot of resources on how to create a complex one but not that many about creating a simple one. While the pieces that make up RecyclerView
may seem confusing at first, they are fairly straightforward once you understand them.
This blog post goes through the steps of creating a simple RecyclerView
that displays the names of different types of flowers. …
When we work with types that can be added, removed, compared, or concatenated we end up with verbose code that we need to write again and again. In Kotlin, we can write more expressive and concise code for these types with the help of operator overloading.
One thing I love as much as I love Android is singing in a choir, so let’s use an example of a choir of singers to illustrate the benefits of operator overloading. Let’s say that we have a choir of singers and we want to add another singer to our choir. …