RecyclerAdapter.kt
class RecyclerAdapter (private var titles: List<String>, private var details: List<String>, private var images:List<Int>) :
RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val itemTitle: TextView = itemView.findViewById(R.id.tv_title)
val itemDetail: TextView = itemView.findViewById(R.id.tv_description)
val itemPicture: ImageView = itemView.findViewById(R.id.iv_image)
init {
itemView.setOnClickListener{ v: View ->
if (position == 0)
Toast.makeText(itemView.context, "You Clicked On Item #1 ", Toast.LENGTH_SHORT).show()
if (position == 4)
Toast.makeText(itemView.context, "You Clicked On Item #77 ", Toast.LENGTH_SHORT).show()
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.item_layout,parent,false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return titles.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemTitle.text = titles[position]
holder.itemDetail.text = details[position]
holder.itemPicture.setImageResource(images[position])
}
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
private var titlesList = mutableListOf<String>()
private var descList = mutableListOf<String>()
private var imagesList = mutableListOf<Int>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
postToList()
val mod = findViewById<RecyclerView>(R.id.rrcycle)
mod.layoutManager = LinearLayoutManager(this)
mod.adapter = RecyclerAdapter(titlesList, descList,imagesList )
}
private fun addToList(title: String, description: String, image: Int) {
titlesList.add(title)
descList.add(description)
imagesList.add(image)
}
private fun postToList() {
for (i in 1..25) {
addToList("Title $i", "Description $i", R.mipmap.ic_launcher_round)
}
}
}
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="4dp"
android:clickable="true"
android:focusable="true"
android:foreground="?selectableItemBackground"
app:cardCornerRadius="10dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl_wrapper"
android:padding="20dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_image"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:src="@mipmap/ic_launcher_round"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="@+id/iv_image"
android:layout_alignBottom="@id/iv_image"
android:layout_toEndOf="@id/iv_image"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="Title"
android:textColor="#000000"
android:textSize="20sp"
/>
<TextView
android:id="@+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="This is a description"
android:textColor="#6A6A6A"
android:textSize="20sp"
/>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/rrcycle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
tools:itemCount="17"
tools:listitem="@layout/item_layout" />
</RelativeLayout>
0 Comments