Skip to content

Commit

Permalink
Handle the case where position might return -1 (RecyclerView.NO_POSIT…
Browse files Browse the repository at this point in the history
…ION)
  • Loading branch information
hafizrahman committed Jan 21, 2025
1 parent 438ecaf commit 2d4a970
Showing 1 changed file with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.woocommerce.android.ui.orders

import android.view.MotionEvent
import android.view.View
import androidx.recyclerview.selection.ItemDetailsLookup
import androidx.recyclerview.widget.RecyclerView
import com.woocommerce.android.ui.orders.list.OrderListAdapter
Expand All @@ -10,17 +11,24 @@ class DefaultOrderListItemLookup(private val recyclerView: RecyclerView) : ItemD
override fun getItemDetails(event: MotionEvent): ItemDetails<Long>? =
recyclerView
.findChildViewUnder(event.x, event.y)
?.let { view ->
recyclerView.getChildViewHolder(view)?.let { viewHolder ->
val position = viewHolder.bindingAdapterPosition
val item = (recyclerView.adapter as? OrderListAdapter)?.currentList?.get(position)
if (item is OrderListItemUI) {
DefaultOrderItemDetailsLookup(position, item.orderId)
} else {
null
}
?.let { view -> getDetailsFromView(view) }

private fun getDetailsFromView(view: View): ItemDetails<Long>? {
val viewHolder = recyclerView.getChildViewHolder(view) ?: return null
val position = viewHolder.bindingAdapterPosition

return when {
position == RecyclerView.NO_POSITION -> null
else -> {
val item = (recyclerView.adapter as? OrderListAdapter)?.currentList?.get(position)
if (item is OrderListItemUI) {
DefaultOrderItemDetailsLookup(position, item.orderId)
} else {
null
}
}
}
}
}

class DefaultOrderItemDetailsLookup(
Expand Down

0 comments on commit 2d4a970

Please sign in to comment.