@Override public int getItemViewType(int position) { final int contactTileAdapterCount = mContactTileAdapter.getCount(); final int contactEntryListAdapterCount = mContactEntryListAdapter.getCount(); // There should be four kinds of types that are usually used, and one more exceptional // type (IGNORE_ITEM_VIEW_TYPE), which sometimes comes from mContactTileAdapter. // // The four ordinary view types have the index equal to or more than 0, and less than // mContactTileAdapter.getViewTypeCount()+ mContactEntryListAdapter.getViewTypeCount() + 2. // (See also this class's getViewTypeCount()) // // We have those values for: // - The view types mContactTileAdapter originally has // - The view types mContactEntryListAdapter originally has // - mAccountFilterHeaderContainer ("all" section's account header), and // - mLoadingView // // Those types should not be mixed, so we have a different range for each kinds of types: // - Types for mContactTileAdapter ("tile" and "frequent" sections) // They should have the index, >=0 and <mContactTileAdapter.getViewTypeCount() // // - Types for mContactEntryListAdapter ("all" sections) // They should have the index, >=mContactTileAdapter.getViewTypeCount() and // <(mContactTileAdapter.getViewTypeCount() + mContactEntryListAdapter.getViewTypeCount()) // // - Type for "all" section's account header // It should have the exact index // mContactTileAdapter.getViewTypeCount()+ mContactEntryListAdapter.getViewTypeCount() // // - Type for "loading" view used during "all" section is being loaded. // It should have the exact index // mContactTileAdapter.getViewTypeCount()+ mContactEntryListAdapter.getViewTypeCount() + 1 // // As an exception, IGNORE_ITEM_VIEW_TYPE (-1) will be remained as is, which will be used // by framework's Adapter implementation and thus should be left as is. if (position < contactTileAdapterCount) { // For "tile" and "frequent" sections return mContactTileAdapter.getItemViewType(position); } else if (position == contactTileAdapterCount) { // For "all" section's account header return mContactTileAdapter.getViewTypeCount() + mContactEntryListAdapter.getViewTypeCount(); } else { // For "all" section if (mContactEntryListAdapter.isLoading()) { // "All" section is being loaded. return mContactTileAdapter.getViewTypeCount() + mContactEntryListAdapter.getViewTypeCount() + 1; } else { // "-1" for mAccountFilterHeaderContainer final int localPosition = position - contactTileAdapterCount - 1; final int type = mContactEntryListAdapter.getItemViewType(localPosition); // IGNORE_ITEM_VIEW_TYPE must be handled differently. return (type < 0) ? type : type + mContactTileAdapter.getViewTypeCount(); } } }
@Override public int getViewTypeCount() { // "+2" for mAccountFilterHeaderContainer and mLoadingView return (mContactTileAdapter.getViewTypeCount() + mContactEntryListAdapter.getViewTypeCount() + 2); }