Hey I have multiple layout in Recyclerview. I want to change into view binding. I have multiple layout and inside that have same id in all layout, only difference is position is different. So How can i make view Holder for that. I want to avoid multiple view holder. I tried don't want to use this Multiple View Holder Is there any possibility to do that? Because all code are same in viewholder. Thanks
AdapterClass.kt
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
class AdapterClass(private val horizontal: Boolean = false) : RecyclerView.Adapter<AdapterViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AdapterViewHolder {
val inflatedView: View = if (horizontal) {
LayoutInflater.from(parent.context).inflate(R.layout.horizontal_layout, parent, false)
} else {
LayoutInflater.from(parent.context).inflate(R.layout.vertical_layout, parent, false)
}
return AdapterViewHolder(inflatedView)
}
override fun onBindViewHolder(holder: AdapterViewHolder, position: Int) {
holder.bingImage(position)
}
}
.........
}
AdapterViewHolder.kt
import android.graphics.drawable.Drawable
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
class AdapterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bingImage(position: Int) {
with(itemView) {
val color = if (isSelected) {
itemView.context.resources.getColor(R.color.blue)
} else {
itemView.context.resources.getColor(R.color.green)
}
main_container.setBackgroundColor(color)
}
}
}
Vertical.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Horizontal.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>