Developer
Developer

Reputation: 1

Changing LinearLayoutManager to StaggeredGridLayoutManager causing index out of bound error

if (parentMode.type.equals("list", true)) { with(binding.parentRV) {
   
   val fifteenDp = Dimension.convertDpToPixels(15f, binding.root.context)

   layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.HORIZONTAL)

// layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)

   if (this.itemDecorationCount > 0)
      removeItemDecorationAt(0)

   addItemDecoration(
      EqualSpacingItemDecoration(
         Dimension.convertDpToPixels(0f, context),
         EqualSpacingItemDecoration.STAGGERED_VERTICAL,
         true,
         true,
         fifteenDp,
         fifteenDp
       )
   )
  }
}

When layoutManager is set to LinearLayout it is working perfectly, but I am changing it to StaggeredGridLayout it is givig index out of bound error when scrolling the items.

I tried putting validation on getItemCount() method but that doesn't worked. Suggest any solution that might be causing this error.

Upvotes: 0

Views: 44

Answers (0)

Related Questions