Make Easy Recycler View With BRVAH — Kotlin

Izal Syafrizal
Nov 6 · 3 min read

BRVAH It’s a great open source project on Github. Its main function is to help us use Recyclerview control more efficiently and to deal with the Adapter of common requirements in the project. It’s very convenient to use. More introduction can be given. BRVAH official website See.

BRVAH is mainly designed for Adapter.

BRVAH provides us with BaseQuick Adapter in general, and Adapter in several specific requirements, BaseMultiItemQuick Adapter for complex class layout lists, BaseItemDraggable Adapter for dragging and dropping class lists, and BaseSection QuickAdapter for lists with Section Header View.

For this RecyclerView example , I’ll be use a list of animals into RecyclerView and user BRVAH as helper for RecyclerView adapter


Start a new project with an Empty Activity at first .

Setting the build.gradle configuration

First add the repositories in build.gradle:

allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}

Then add in the depencies for BRVAH and RecyclerView :

dependencies {...
implementation 'androidx.recyclerview:recyclerview:1.0.0' implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:VERSION_CODE'
}

you can get the latest version of VERSION_CODE is available Here.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_animal_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

To display each row in the RecyclerView we need to create a new layout file . Create a new file called “animal_list_item”.

animal_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<TextView
android:id="@+id/tv_animal_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="20dp"
/>

</LinearLayout>

Don’t forget to set the layout_height in the linear layout to “wrap_content” so it doesn’t only show one TextView per page.

After setting the layout we need to make the model for our ArrayList, let’s name it with Animal.kt

Animal.kt

class Animal(var name: String?)

After we make the model , we are going to make the Adapter for our RecyclerView .

Create an Kotlin Class with name AnimalAdapter.

Inside this class we just only need to take the Layout id and the List of our animal

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

class AnimalAdapter(layoutResId: Int, data: List<Animal> ) : BaseQuickAdapter<Animal, BaseViewHolder>(layoutResId, data) {
}

and also in this class we convert from object and put it into the layout for each row inside the convert function.

override fun convert(helper: BaseViewHolder, item: Animal) {
helper.setText(R.id.tv_animal_type , item.name)
}

so our adapter will be like this

AnimalAdapter.kt

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

class AnimalAdapter(layoutResId: Int, data: List<Animal> ) : BaseQuickAdapter<Animal, BaseViewHolder>(layoutResId, data) {
// Place data from Object(item) to the View(helper)
override fun convert(helper: BaseViewHolder, item: Animal) {
helper.setText(R.id.tv_animal_type , item.name)
}
}

After make our Adapter and Model for RecyclerView we are going to place it inside the MainActivity.

So inside the MainActivity we are going to create an ArrayList

val animals : ArrayList<Animal> = ArrayList()

after make the list we need to make a function to add an item to the list

fun addAnimals(){
animals.add(Animal("Anjing"))
animals.add(Animal("Ayam"))
animals.add(Animal("Bebek"))
animals.add(Animal("Ular"))
animals.add(Animal("Katak"))
animals.add(Animal("Babi"))
animals.add(Animal("Kuda Nil"))
animals.add(Animal("Sapi"))
animals.add(Animal("Kucing"))
animals.add(Animal("Ulat"))
}

and we need to call it onCreate

Add a Layout Manager and set it to our RecyclerView in the onCreate function , which is we are going to use LinearLayoutManager , if you want to know what is Layout Manager is you can read Here.

rv_animal_list.layoutManager = LinearLayoutManager(this)

We also set the Adapter that we already make (AnimalAdapter.kt) to the RecyclerView in the onCreate unction, also we are going to pass the two parameter for our Adapter which is the layout id and the list.

rv_animal_list.adapter = AnimalAdapter(R.layout.animal_list_item,animals)

So our MainActivity will be look like this :

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
val animals : ArrayList<Animal> = ArrayList()


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
addAnimals()
rv_animal_list.layoutManager = LinearLayoutManager(this)
rv_animal_list.adapter = AnimalAdapter(R.layout.animal_list_item,animals)
}
}

fun addAnimals(){
animals.add(Animal("Anjing"))
animals.add(Animal("Ayam"))
animals.add(Animal("Bebek"))
animals.add(Animal("Ular"))
animals.add(Animal("Katak"))
animals.add(Animal("Babi"))
animals.add(Animal("Kuda Nil"))
animals.add(Animal("Sapi"))
animals.add(Animal("Kucing"))
animals.add(Animal("Ulat"))
}
}

That’s it ! Now we run the project !

The Full source code is available here.

Izal Syafrizal

Written by

https://www.linkedin.com/in/muhammad-syafrizal-1abb70120/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade