Hi friends hope you are doing well . In this article i am gonna show you one of the most used view component in android. RecyclerView In Kotlin
This article will show your how to use kotlin data class to create objects and to define custom hashcode and equals methods
First Step is to create an activity with support for kotlin
After that add in app dependencies
1 |
implementation 'com.android.support:recyclerview-v7:27.1.1' |
For simplicity i have added comment to explain about code
My onCreate function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
override fun onCreate(savedInstanceState: Bundle?) { // ? in Bundle? means it is nullable || we can assign null to it super.onCreate(savedInstanceState) val bar = supportActionBar bar?.title = "Kotlin List" bar?.setBackgroundDrawable(ColorDrawable(Color.parseColor("#E43F37"))) val list = RecyclerView(this) // val implies that this filed is immutable setContentView(list) val userList = ArrayList<User>() userList.add(User("Vishnu")) userList.add(User("Edward")) userList.add(User("Ravi")) userList.add(User("Raghu")) userList.add(User("John Doe")) userList.add(User("Vijay")) userList.add(User("Vinay")) list.layoutManager = LinearLayoutManager(this) list.adapter = MyAdapter(userList) } |
I am not using layout file Just Created a Recyclerview and set it as content for this activity .There is nothing special in it you can define it however you like.
–>after that created dummy list with some user
–>As you can see while creating any object we dont need to add (new ) keyword before class name in kotlin
Now its time to have a look of our data class
data classes is another reason to love kotlin. this class generate getter, setter, equal and hashcode for you
As i have already defined isSelected as false it is not compulsary to pass while creating object of user isn’t it great (For me its just another reason to ditch Java)
1 |
data class User(val name: String, var isSelected: Boolean = false) |
or if you want to provide different custom for hashcode generation and equal you can do it like this
1 2 3 4 5 6 7 8 |
data class User(val name: String, val isSelected: Boolean = false){ override fun equals(other: Any?): Boolean { return name.equals(name,true) // here true is equavalent to equalsIgnoreCase } override fun hashCode(): Int { return this.name.hashCode() } } |
Finally our RecyclerView Adapter
Here i am just creating a simple class which extending RecyclerView.Adapter and passing Viewholder in it (Common RecyclerView Work)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class MyAdapter(val users: ArrayList?) : RecyclerView.Adapter() { //In Kotlin we can do something like this (In single line) // override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.single_user_row, parent, false)) //OR Can do like this .It all upto you override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.single_user_row, parent, false)) } //as i am passing ? flag in constructor //Again we can write this in same line /*override fun getItemCount(): Int { return users?.size ?: 0 } */ override fun getItemCount() = users?.size ?: 0 override fun onBindViewHolder(holder: MyViewHolder, position: Int) { holder.name.text = users?.get(position)?.name // Here i am checking for if list is null or if item is null all in single line //Multiple selection val single = users?.get(position) holder.itemView.setOnClickListener { if (single?.isSelected ?: false) { //user elvis (?:) operator to define default value i case item is null holder.itemView.isSelected = false single?.isSelected = false } else { holder.itemView.isSelected = true single?.isSelected = true } } } class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val name = itemView.findViewById(R.id.tvName) } } |
Thats all you need to make RecyclerView in Kotlin With Custom object list
Resources Used:
single_user_row (in layout)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tvName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:background="@drawable/bg_reactangle_selector" android:padding="12dp" android:text="" android:textColor="#FFFFFF" android:textStyle="bold"> </TextView> |
bg_reactangle_selector.xml (in drawable)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="false" android:state_selected="true"> <shape android:shape="rectangle"> <solid android:color="#E43F37" /> <corners android:radius="2dp"/> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="#84B154" /> <corners android:radius="2dp"/> </shape> </item> </selector> |
Have a wonderfull day bye.