In the Normal recycler view, if we use getItemViewType that helps us to handle position layout likely if your recycler view binding the same layout after scroll that function helps us to tackle that problem.
In my case I'm using expandable recycler view you can easily find what is expandable recycler view on google Here is the code that causes an error to view the right view on screen.
so problem is when i scroll checkbox automatic checked.
Category Adapter class:
class CategoryAdapter(
var context: Context? = null,
onFilterClickListener: OnFilterClickListener) :
ExpandableRecyclerAdapter<CategoryAdapter.CategoryViewHolder, CategoryAdapter.CategoryListViewHolder>() {
var listener: OnFilterClickListener = onFilterClickListener
interface OnFilterClickListener {
fun onChildClickListener(childListItem: CategoryList, checked: Boolean)
}
override fun onCreateParentViewHolder(
parentViewGroup: ViewGroup
): CategoryViewHolder {
val view: View = LayoutInflater.from(parentViewGroup.context)
.inflate(R.layout.main_ctg_item, parentViewGroup, false)
return CategoryViewHolder(view)
}
override fun onCreateChildViewHolder(parentViewGroup: ViewGroup): CategoryListViewHolder {
val viewChild = LayoutInflater.from(parentViewGroup.context)
.inflate(R.layout.sub_ctg_item, parentViewGroup, false)
return CategoryListViewHolder(viewChild)
}
override fun onBindParentViewHolder(
parentViewHolder: CategoryViewHolder,
position: Int,
parentListItem: ParentListItem
) {
val data = parentListItem as Category
parentViewHolder.bind(data)
}
override fun onBindChildViewHolder(
childViewHolder: CategoryListViewHolder,
position: Int,
childListItem: Any
) {
getItemViewType(0)
val data = childListItem as CategoryList
childViewHolder.bind(data)
}
inner class CategoryViewHolder(itemView: View) : ParentViewHolder(itemView) {
private lateinit var animation: RotateAnimation
var titleSelected: String = ""
fun bind(category: Category) {
itemView.findViewById<TextView>(R.id.GroupTv).text = category.name
}
override fun onExpansionToggled(expanded: Boolean) {
super.onExpansionToggled(expanded)
Log.i("LOGGG", titleSelected)
animation = if (expanded)
RotateAnimation(
180f,
0f,
RotateAnimation.RELATIVE_TO_SELF,
0.5f,
RotateAnimation.RELATIVE_TO_SELF,
0.5f
)
else
RotateAnimation(
-1 * 180f,
0f,
RotateAnimation.RELATIVE_TO_SELF,
0.5f,
RotateAnimation.RELATIVE_TO_SELF,
0.5f
)
animation.duration = 200
animation.fillAfter = true
itemView.findViewById<ImageView>(R.id.btnExpand).startAnimation(animation)
titleSelected = itemView.findViewById<TextView>(R.id.GroupTv).text.toString()
Log.i("LOGGG", titleSelected)
}
override fun setExpanded(expanded: Boolean) {
super.setExpanded(expanded)
if (expanded) itemView.findViewById<ImageView>(R.id.btnExpand).rotation = 180f
else itemView.findViewById<ImageView>(R.id.btnExpand).rotation = 0f
}
}
inner class CategoryListViewHolder(view: View) : ChildViewHolder(view) {
fun bind(categoryList: CategoryList) {
itemView.findViewById<CheckBox>(R.id.filterTv).text = categoryList.name
itemView.findViewById<CheckBox>(R.id.filterTv).tag = position
itemView.findViewById<CheckBox>(R.id.filterTv).setOnClickListener(View.OnClickListener {
listener.onChildClickListener(
categoryList,
itemView.findViewById<CheckBox>(R.id.filterTv).isChecked
)
Toast.makeText(itemView.context, categoryList.name, Toast.LENGTH_SHORT).show()
})
}
}}
CategoryList Class:
data class CategoryList(val name: String, val id: String,val title : String)
Category class:
data class Category(val name: String, val itemList: List<CategoryList>) : ParentListItem {
override fun getChildItemList(): List<*> = itemList
override fun isInitiallyExpanded(): Boolean = false}