I have a contact list that are separated by group header. So I have used getItemViewType in my adapter :
@Override
public int getItemViewType(int position) {
ContactItem contactItem = mContacts.get(position);
if (contactItem.getContactSeparator() == null) {
if (contactItem.getContact().getContactType() == ContactType.SINGLE) {
if (mShowSeparator) {
return TYPE_CONTACT;
}
return TYPE_SEARCH_CONTACT;
}
if (mShowSeparator) {
return TYPE_CONTACT_MULTIPLE;
}
return TYPE_SEARCH_CONTACT_MULTIPLE;
}
return TYPE_SEPARATOR;
}
As you see I check if Contact Separator is null or not. So here is my model class :
@Parcelize
class ContactItem @JvmOverloads constructor(
var contact: Contact? = null,
var contactSeparator: String? = null
) : Parcelable
Is there any cleaner solution for my model class when we have multiple items in our adapter?
For instance for Contact type I have a enum :
enum class ContactType {
SINGLE,
MULTIPLE
}
And set it up in background thread :
if (numbers.size() > 1) {
contact.setContactType(ContactType.MULTIPLE);
}
But this solution seems pretty much the same as 1st one. What is your suggestion?
Source code can be found here : https://github.com/alirezaeiii/Rebtel-Contacts-Clone/tree/master
Does something like this solve your problem? I have separated the "separator" implementation