-
-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ViewHolder safely use adapterPosition #239
Comments
From what I can read, this is not an issue in Splitties. |
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 Regardless, the method signature is not really simpler than an |
Maybe my personal preferences, I don't like putting 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
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
The text was updated successfully, but these errors were encountered: