Xigong93/ExpandableRecyclerView

Unable to have different layouts for child view

wishygupta opened this issue · 2 comments

Hi,

I am trying to have different layout's for child view. But it keeps popping either the first layout or header view.
This is the adapter class
https://gist.github.com/wishygupta/d5f6aba712b97eee6797700347cfc24c

Hi,Here is wrong in your code.

  override fun getChildItemViewType(groupPosition: Int, childPosition: Int): Int {
        if (groupPosition == 0)
            return super.getChildItemViewType(groupPosition, VIEW_OFFER)
        if (groupPosition == 1)
            return super.getChildItemViewType(groupPosition, VIEW_PROPERTY)
        if (groupPosition == 2)
            return super.getChildItemViewType(groupPosition, VIEW_CLASS)
        if (groupPosition == 3)
            return super.getChildItemViewType(groupPosition, VIEW_PAYMENT)
        return super.getChildItemViewType(groupPosition, childPosition)
    }

I thought it's maybe.

  override fun getChildItemViewType(groupPosition: Int, childPosition: Int): Int {
        if (groupPosition == 0)
            return VIEW_OFFER
        if (groupPosition == 1)
            return VIEW_PROPERTY
        if (groupPosition == 2)
            return VIEW_CLASS
        if (groupPosition == 3)
            return VIEW_PAYMENT
        throw RuntimeException("unknow type") 
    }

Note this. Group 's viewType must be positive .Child 's viewType must be negative or zero. You also can change that by overwrite function isGroup.

    /**
     * Distinguish group item or child item by viewType.
     * By default groupViewType is positive , childViewType is negative or zero.
     */
    open fun isGroup(viewType: Int): Boolean = viewType > 0

Thank you for the quick help.