Open vincent-paing opened 4 years ago
From what I can read, this is not an issue in Splitties. If what you want is to make a proposal of a new extension for Splitties, please, be clear about it, and give a use case where appropriate (for the extension you showed, it is appropriate to give a use case as I never needed this so far), while keeping the details level in control.
Sorry for the confusion caused on my part. I was proposing new extensions for Splitties. The use case would be just that it could cut down lines such as these
if (adapterPosition != RecyclerView.NO_POSITION) {
//Lines of codes here
} else {
//Lines of codes here
}
And allow easier usage of position safely. I've been using such extensions a lot on my projects, not sure if it might be necessary for others. Feel free to close it if you feel like it's not needed.
I personally never needed to check the position of a ViewHolder
, so I have trouble understanding in which case you'd need it.
Regardless, the method signature is not really simpler than an if
to me, so it's unlikely I'll bring this to Splitties public API, but I'm curious where you'd need to get the position of a ViewHolder
, and what you'd do when you don't have it or when you have it @vincent-paing.
I personally never needed to check the position of a ViewHolder, so I have trouble understanding in which case you'd need it.
Maybe my personal preferences, I don't like putting setOnClickListener
in onBindViewHolder
because such listener doesn't need to binded each time holders are recycled. So I put them under onCreateViewHolder
. This way, it's more performant since listener are not being added again and again. However onCreateViewHolder
won't have position in its method signature since it's not tied to position.
In such case, I use this following code
val view = //inflate
val viewHolder = ViewHolder(view)
viewHolder.apply {
itemView.setOnClickListener {
if (holder.position != RecyclerView.NO_POSITION) {
//do stuffs here
}
}
}
return viewHolder
And what you'd do when you don't have it or when you have it
It's a very very rare case and wouldn't happen unless you are updating the items in adapter frequently. Currently I only place Timber logs there but you can also do stuffs like providing error call back, showing "Item not exist" Toast etc.
ViewHolder
has function that can get its position throughadapterPosition
. However on rare cases, user scroll,adapterPosition
could returnRecyclerView
could returnNO_POSITION
, which is a-1
. To safely useadapterPosition
, we can check withif (adapterPosition != RecyclerView.NO_POSITION
I came up with this extension function to safely use it without the hassle of if check