Hello Friends,
Today I am sharing the demo of RecyclerView in Kotlin.
A RecyclerView is essentially a ViewGroup of containers called ViewHolders which
populate a particular item.
So lets first familiar with RecyclerView and What RecyclerView requires:
1. It requires a set of data objects to work with
2. An xml file of the individual view item
3. An adapter to bind that data to the views shown in the ViewHolders
4. ViewHolder to populate the UI from the xml item file
Getting Started
Download code from here
Hope this will helps someone.
Enjoy coding.... :)
Today I am sharing the demo of RecyclerView in Kotlin.
A RecyclerView is essentially a ViewGroup of containers called ViewHolders which
populate a particular item.
So lets first familiar with RecyclerView and What RecyclerView requires:
1. It requires a set of data objects to work with
2. An xml file of the individual view item
3. An adapter to bind that data to the views shown in the ViewHolders
4. ViewHolder to populate the UI from the xml item file
Getting Started
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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/recyclerView" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_marginStart="8dp" | |
android:layout_marginTop="8dp" | |
android:layout_marginEnd="8dp" | |
android:layout_marginBottom="8dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<TextView | |
android:id="@+id/tvName" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="8dp" | |
android:layout_marginTop="8dp" | |
android:layout_marginEnd="8dp" | |
android:padding="5dp" | |
android:text="Mukesh Yadav" | |
android:textStyle="bold" | |
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<TextView | |
android:id="@+id/tvEmail" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="5dp" | |
android:layout_marginTop="4dp" | |
android:layout_marginEnd="8dp" | |
android:padding="5dp" | |
android:text="himky02@gmail.com" | |
android:textStyle="bold" | |
app:layout_constraintTop_toBottomOf="@+id/tvName" | |
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" /> | |
<TextView | |
android:id="@+id/tvMobile" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="5dp" | |
android:layout_marginTop="4dp" | |
android:layout_marginEnd="8dp" | |
android:padding="5dp" | |
android:text="9555xxxx59" | |
app:layout_constraintTop_toBottomOf="@+id/tvEmail" | |
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</androidx.cardview.widget.CardView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.android.developer.soulutions.recyclerview | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.LinearLayout | |
import androidx.recyclerview.widget.DividerItemDecoration | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import androidx.recyclerview.widget.RecyclerView | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
recyclerView.layoutManager = LinearLayoutManager(this,RecyclerView.VERTICAL,false) | |
recyclerView.addItemDecoration(DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL)) | |
val users = ArrayList<User>() | |
users.add(User("Mukesh Yadav",30,"himky02@gmail.com","955xxxxx59")) | |
users.add(User("ABC Limited",30,"abc@gmail.com","880xxxxx53")) | |
users.add(User("XYZ LTD",30,"xyz@gmail.com","966xxxxx59")) | |
users.add(User("Muesh",30,"mukesh@gmail.com","955xxxxx99")) | |
users.add(User("Android",30,"android@gmail.com","955xxxxx89")) | |
users.add(User("Developer",30,"developer@gmail.com","955xxxxx49")) | |
val adapter = UserAdapter(users) | |
recyclerView.adapter = adapter | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.android.developer.soulutions.recyclerview | |
import android.os.Parcel | |
import android.os.Parcelable | |
/** | |
* Created by Mukesh on 10/7/2019. | |
* https://www.androiddevelopersolutions.com/ | |
*/ | |
data class User(val name: String, val age : Int, | |
val email: String, val phone: String) : Parcelable { | |
constructor(parcel: Parcel) : this( | |
parcel.readString(), | |
parcel.readInt(), | |
parcel.readString(), | |
parcel.readString() | |
) | |
override fun writeToParcel(parcel: Parcel, flags: Int) { | |
parcel.writeString(name) | |
parcel.writeInt(age) | |
parcel.writeString(email) | |
parcel.writeString(phone) | |
} | |
override fun describeContents(): Int { | |
return 0 | |
} | |
companion object CREATOR : Parcelable.Creator<User> { | |
override fun createFromParcel(parcel: Parcel): User { | |
return User(parcel) | |
} | |
override fun newArray(size: Int): Array<User?> { | |
return arrayOfNulls(size) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.android.developer.soulutions.recyclerview | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.TextView | |
import androidx.recyclerview.widget.RecyclerView | |
/** | |
* Created by Mukesh on 10/7/2019. | |
* https://www.androiddevelopersolutions.com/ | |
*/ | |
class UserAdapter(val userList : ArrayList<User>) : RecyclerView.Adapter<UserAdapter.ViewHolder>(){ | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserAdapter.ViewHolder { | |
val v = LayoutInflater.from(parent.context).inflate(R.layout.item_user, parent, false) | |
return ViewHolder(v) | |
} | |
override fun getItemCount(): Int { | |
return userList.size | |
} | |
override fun onBindViewHolder(holder: UserAdapter.ViewHolder, position: Int) { | |
holder.bindItems(userList[position]) | |
} | |
//the class is hodling the list view | |
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | |
fun bindItems(user: User) { | |
val tvName = itemView.findViewById(R.id.tvName) as TextView | |
val tvEmail = itemView.findViewById(R.id.tvEmail) as TextView | |
val tvPhone = itemView.findViewById(R.id.tvMobile) as TextView | |
tvName.text = user.name | |
tvEmail.text = user.email | |
tvPhone.text = user.phone | |
} | |
} | |
} |
Download code from here
Hope this will helps someone.
Enjoy coding.... :)
0 comments:
Post a Comment