I have some diff blocks generated with RecyclerView, according the design the last two blocks should have another text size. I do this programmatically:
when(position){
0 -> {
holder.catCard.setCardBackgroundColor(Color.parseColor("#FF5668"))
}
1 -> {
holder.catCard.setCardBackgroundColor(Color.parseColor("#18C2E9"))
}
3 -> {
holder.catCard.setCardBackgroundColor(Color.parseColor("#18C2E9"))
40F.also { holder.catTitle.textSize = it }
}
}
inner class ViewHolder(itemView:View): RecyclerView.ViewHolder(itemView), View.OnClickListener{
var catImage: ImageView = itemView.findViewById(R.id.catImage)
var catTitle: TextView = itemView.findViewById(R.id.catTitle)
var catCard: CardView = itemView.findViewById(R.id.catCardItem)
var catTitleWrapper: LinearLayout = itemView.findViewById(R.id.catTitleWrapper)
override fun onClick(viewType: View?) {
listener.invoke(adapterPosition)
}
}
But I also have to change the view one of the LinearLayout:
var catTitleWrapper: LinearLayout = itemView.findViewById(R.id.catTitleWrapper)
the variable from inner class. I try to implement some solution but it does not work at all. Please help me to solve this if it is possible.
CodePudding user response:
To change the last item layout params, you will have to check if the RecyclerView method "onBindViewHolder" has been called in the last item from the list, to do so, in your inner class ViewHolder:
inner class ViewHolder(itemView:View): RecyclerView.ViewHolder(itemView), View.OnClickListener{
var catImage: ImageView = itemView.findViewById(R.id.catImage)
var catTitle: TextView = itemView.findViewById(R.id.catTitle)
var catCard: CardView = itemView.findViewById(R.id.catCardItem)
var catTitleWrapper: LinearLayout = itemView.findViewById(R.id.catTitleWrapper)
if (bindingAdapterPosition 1 == itemCount) {
val catTitleWrapperLayoutParams = catTitleWrapper.layoutparms
catTitleWrapperLayoutParams.width = $YOUR_WIDTH: Int
}
override fun onClick(viewType: View?) {
listener.invoke(adapterPosition)
}
}
Basically we check if the RecyclerView variable bindingAdapterPosition is equals to itemCount, if it is so means we are on the last item, and we proceed to apply the layout params to the mentioned wrapper. We add 1 to bindingAdapterPosition because it will return the indexes of the item, meaning the first one will be 0. I wrote it by hand, let me know if it works :D