androidkotlinandroid-recyclerviewtracking

Filter RecyclerView on the basis of ItemViewType


I have a fetched a list of Offers (OfferBaseModel). Offers can be of two types Personal or Favourite . Now I want to show these on tab selection

open class OfferBaseModel -> Parent Class
data class PersonalOfferModel(val personalOffer: PersonalOffer) : OfferBaseModel()
data class FavoriteOfferModel(val product: Product) : OfferBaseModel() 

I have to use same RecyclerView for both the items. If I click on PersonOffer Tab. RecyclerView should filter only PersonalOfferItemViewType and when I click on FavouriteOffer Tab it should show only FavouriteItemViewType. And when I cick on the third tab(All) . It should show both type of data

I have to filter the same list as I have to tracked the offer viewed once in any tab.

I don't want to provide new list on tab selection . Only filter on the basis of viewItemType.

All Offer Tab || Favourite Offer Tab || Personal Offer Tab

But RecyclerView under it remains same along with same list provided to it once. Only filter on selection on above tabs.

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    when (getItemViewType(position)) {
        TYPE_PERSONAL_SECTION -> (holder as PersonalOfferViewHolder).bind(position)
        TYPE_FAVOURITE_SECTION -> (holder as FavouriteOfferViewHolder).bind(position)
    }
}

override fun getItemViewType(position: Int): Int {
    return if (sections[position].shopSectionEntity.styling == "SecondarySection") {
        TYPE_PERSONAL_SECTION
    } else {
        TYPE_FAVOURITE_SECTION
    }
}

As I have created a TrackableRecyclerView. Each item must be tracked once. I mean if a Personal Offer is viewed in Personal Offer Tab. It's analytics tracking shouldn't be called again If same product is displayed in All Section Tab


Solution

  • It is very difficult to make inferences from existing codes, but I would like to offer some logic.

    enum class ViewType {
        PERSONAL, FAVOURITE, BOTH
    } // your condition view
    
    var currentViewType = ViewType.BOTH // default view
    
    fun updateViewType(viewType: ViewType) {
        currentViewType = viewType
        notifyDataSetChanged() // or diffUtil
    }
    
    override fun getItemViewType(position: Int): Int {
        return when (currentViewType) {
            ViewType.PERSONAL -> if (sections[position] is PersonalOfferModel) {
                TYPE_PERSONAL_SECTION
            } else -1
            ViewType.FAVOURITE -> if (sections[position] is FavoriteOfferModel) {
                TYPE_FAVOURITE_SECTION
            } else -1
            else -> if (sections[position] is PersonalOfferModel) {
                TYPE_PERSONAL_SECTION
            } else if (sections[position] is FavoriteOfferModel) {
                TYPE_FAVOURITE_SECTION
            } else -1
        }
    }
    

    and your condition buttons

    firstButton.setOnClickListener {
        adapter.updateViewType(ViewType.PERSONAL)
    }
    
    secondButton.setOnClickListener {
        adapter.updateViewType(ViewType.FAVOURITE)
    }
    
    thirdButton.setOnClickListener {
        adapter.updateViewType(ViewType.BOTH)
    }