I was searching for best practices to implement the home screen especially it has different sections, I found that the most common way is to create a recycler view with different view types.
it's working amazing and everything is okay except when the user starts to scroll and the trigger for the next sections start to draw it's blocking the UI thread for while to inflate the layout for the next sections, so, I'd like to ask for any suggestions to improve that situation?
am I missing something?
class HomeAdapter(
val categoriesAdapter: HomeCategoriesAdapter,
val offersAdapter: HomeOffersAdapter,
val brandsAdapter: HomeBrandsAdapter,
val preferencesUtil: PreferencesUtil,
val bestSellAdapter: ProductsAdapter,
val userManager: UserManager
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var userName = userManager.getUserFirstName()
private var homeModel: HomeModel? = null
private val userNameObserver = Observer<UserModel?> {
userName = userManager.getUserFirstName()
notifyItemChanged(0)
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
userManager.userLiveData.observeForever(userNameObserver)
}
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
userManager.userLiveData.removeObserver(userNameObserver)
super.onDetachedFromRecyclerView(recyclerView)
}
override fun getItemViewType(position: Int): Int {
return position + 1
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
1 -> SectionOneVH(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.SectionOneVH,
parent,
false
)
)
2 -> SectionTwoVH(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.SectionTwoVH,
parent,
false
)
)
3 -> SectionThreeVH(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.SectionThreeVH,
parent,
false
)
)
4 -> SectionFourVH(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.SectionFourVH,
parent,
false
)
)
5 -> SectionFiveVH(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.SectionFiveVH,
parent,
false
)
)
6 -> SectionSixVH(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.SectionSixVH,
parent,
false
)
)
7 -> SectionSevenVH(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.SectionSevenVH,
parent,
false
)
)
else -> SectionOneVH(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.SectionOneVH,
parent,
false
)
)
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
// each bind has text & recycler declaration & click lisners in the init block
is SectionOneVH -> holder.bind()
is SectionTwoVH -> holder.bind()
is SectionThreeVH -> holder.bind()
is SectionFourVH -> holder.bind()
is SectionFiveVH -> holder.bind()
is SectionSixVH -> holder.bind()
}
}
override fun getItemCount() = 7
fun pushData(homeModel: HomeModel) {
this.homeModel = homeModel
notifyDataSetChanged()
}
inner class SectionOneVH(private val binding: SectionOneBinding) :
private val context = binding.root.context
fun bind() {
// logic for binding the SectionOneVH
}
}
inner class SectionTwoVH(private val binding: SectionTwoBinding) :
fun bind() {
// logic for binding the SectionTwoVH
}
}
inner class SectionThreeVH(private val binding: SectionThreeBinding) :
fun bind() {
// logic for binding the SectionThreeVH
}
}
inner class SectionFourVH(private val binding: SectionFourBinding) :
fun bind() {
// logic for binding the SectionFourVH
}
}
inner class SectionFiveVH(private val binding: SectionFiveBinding) :
fun bind() {
// logic for binding the SectionFiveVH
}
}
inner class SectionSixVH(private val binding: SectionSixBinding) :
fun bind() {
// logic for binding the SectionFourVH
}
}
inner class SectionSevenVH(private val binding: SectionSevenBinding) :
fun bind() {
// logic for binding the SectionFourVH
}
}
}