I’m creating this type of design using a horizontal RecyclerView with GridLayoutManager in Android.
Need like design
Current Out Put
RecyclerView Declaration Code
val layoutManager: RecyclerView.LayoutManager =
GridLayoutManager(requireContext(), 2, GridLayoutManager.HORIZONTAL, false)
binding.rvStoreTypesHome.setLayoutManager(layoutManager)
binding.rvStoreTypesHome.adapter = StoreTypesAdapter(requireContext())
Adapter
class StoreTypesAdapter(val context: Context) :
RecyclerView.Adapter<StoreTypesAdapter.ViewHolder>() {
var width = 0
init {
width = context.getDeviceWidth()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding =
StoreTypesAdapterBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val layoutParams = holder.binding.conSTAdapter.layoutParams
layoutParams.width = if (holder.adapterPosition % 2 == 0) {
(width / 2.5).toInt()
} else {
(width / 3.5).toInt()
}
layoutParams.height = ConstraintLayout.LayoutParams.WRAP_CONTENT
holder.binding.conSTAdapter.layoutParams = layoutParams
holder.binding.tvStoreNameSTAdapter.text = "Store Name $position"
}
override fun getItemCount(): Int {
return 10
}
inner class ViewHolder(var binding: StoreTypesAdapterBinding) :
RecyclerView.ViewHolder(binding.root)
}
Set width dynamically using device width but only showing the first
position width. 0,1,2 text is a position that I’m getting in
onBindViewHolder
.