Today i will cook tasty RecyclerView in Kotling way. Minimum consumables, maximum output. Juicy, fragrant, powerful and simple at the same time. Help yourself!

First you need to create an empty project, do not forget to include support for the Kotlin language in the project settings. Then you need to add RecyclerView support to the file  build.gradle  in app module:

Currently the latest version is 28.0.0-alpha3. Sure, over time, the current version will be increased. After that, you can delete the default TextView from the layout file and place RecyclerView instead.:

And it is also worth immediately creating a markup file for the list item and class for storing the items:

Now we need to create adapter:

As can be seen from the code, the list of elements and the callback interface, which will be implemented in the Activity class, are immediately passed to the adapter constructor.
Now you can find RecyclerView in Activity. At Kotlin it is very simple. You need to use the kotlin-android-extensions library. With its help, you can simply import the XML-layout file into the Activity and all elements that have an id will be available in the code by this id:

Now it’s worth creating a list of test data, declare an adapter, assign list to adapter, implement a callback interface and put adapter to RecycltrView:

That’s all, folks. Code can be runned.

Code can be found on my GitHub

Leave a Reply

Close Menu