diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/DefaultOrderListItemLookup.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/DefaultOrderListItemLookup.kt index 51e469c0632..6b34a4c036c 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/DefaultOrderListItemLookup.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/DefaultOrderListItemLookup.kt @@ -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 @@ -10,17 +11,24 @@ class DefaultOrderListItemLookup(private val recyclerView: RecyclerView) : ItemD override fun getItemDetails(event: MotionEvent): ItemDetails? = 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? { + 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(