RecyclerView Android Horizontal - 01

HORIZONTAL RECYCLERVIEW 




1. Take RecyclerView in your xml file


<androidx.recyclerview.widget.RecyclerView
android:layout_margin="15dp"
android:id="@+id/horizontalRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>



 2. Create Layout Resource file. -item_horizontal_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:gravity="center"
android:padding="8dp"
android:layout_margin="5dp"
android:minWidth="120dp"
android:minHeight="140dp"
android:background="@drawable/border">

<com.google.android.material.imageview.ShapeableImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/imageView"
app:shapeAppearanceOverlay="@style/roundImageView"
android:src="@drawable/mm"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/textView"
android:text="Sharwar"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/black"/>

</LinearLayout>




3. Create kotlin data class. name  PersonRecycleData'
data class PersonRecycleData(
val imageResource: Int,
val title: String
)



4. Create kotlin Adapter class. name 'PersonRecycleAdapter'
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class PersonRecycleAdapter(private val dataList: List<PersonRecycleData>) : RecyclerView.Adapter<PersonRecycleAdapter.HorizontalViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HorizontalViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_horizontal_list, parent, false)
return HorizontalViewHolder(view)
}

override fun onBindViewHolder(holder: HorizontalViewHolder, position: Int) {
val item = dataList[position]
holder.imageView.setImageResource(item.imageResource)
holder.textView.text = item.title
}

override fun getItemCount() = dataList.size

class HorizontalViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView = itemView.findViewById(R.id.imageView)
val textView: TextView = itemView.findViewById(R.id.textView)
}
}




5. Now write code in your activity kotlin file
//Horizontal RecycleView
val horizontalItemList = listOf(
PersonRecycleData(R.drawable.mm, "Sharwar"),
PersonRecycleData(R.drawable.mahira,"Rafik"),
PersonRecycleData(R.drawable.mm,"Sam"),
PersonRecycleData(R.drawable.mm,"Iem One"),
PersonRecycleData(R.drawable.mm,"Iem One"),
PersonRecycleData(R.drawable.mm,"Iem One"),
PersonRecycleData(R.drawable.mm,"Iem One"),
PersonRecycleData(R.drawable.mm,"Iem One"),
PersonRecycleData(R.drawable.mm,"Iem One"),
PersonRecycleData(R.drawable.mm,"Iem One"),
PersonRecycleData(R.drawable.mm,"Iem One"),
PersonRecycleData(R.drawable.mm,"Iem One"),
PersonRecycleData(R.drawable.mm,"Iem One"),
PersonRecycleData(R.drawable.mm,"Iem One"),
PersonRecycleData(R.drawable.mm,"Iem Last")
)

val horizontalRecyclerView = findViewById<RecyclerView>(R.id.horizontalRecyclerView)
horizontalRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
horizontalRecyclerView.adapter = PersonRecycleAdapter(horizontalItemList)






PART 2: RecyclerView with Detail View



Extra 1: How to create circular image
Extra 2: How to border shape

Comments