public @NonNull Item getItem(int position) { return getItem(groups, position); }
/** * This idea was copied from Epoxy. :wave: Bright idea guys! * <p> * Find the model that has the given view type so we can create a viewholder for that model. * <p> * To make this efficient, we rely on the RecyclerView implementation detail that {@link * GroupAdapter#getItemViewType(int)} is called immediately before {@link * GroupAdapter#onCreateViewHolder(android.view.ViewGroup, int)}. We cache the last model * that had its view type looked up, and unless that implementation changes we expect to have a * very fast lookup for the correct model. * <p> * To be safe, we fallback to searching through all models for a view type match. This is slow and * shouldn't be needed, but is a guard against RecyclerView behavior changing. */ private Item<VH> getItemForViewType(@LayoutRes int layoutResId) { if (lastItemForViewTypeLookup != null && lastItemForViewTypeLookup.getLayout() == layoutResId) { // We expect this to be a hit 100% of the time return lastItemForViewTypeLookup; } // To be extra safe in case RecyclerView implementation details change... for (int i = 0; i < getItemCount(); i++) { Item item = getItem(i); if (item.getLayout() == layoutResId) { return item; } } throw new IllegalStateException("Could not find model for view type: " + layoutResId); }
@Override public void onBindViewHolder(@NonNull VH holder, int position, @NonNull List<Object> payloads) { Item contentItem = getItem(position); contentItem.bind(holder, position, payloads, onItemClickListener, onItemLongClickListener); }
@Override public int getSpanSize(int position) { try { return getItem(position).getSpanSize(spanCount, position); } catch (IndexOutOfBoundsException e) { // Bug in support lib? TODO investigate further return spanCount; } } };
@Override public long getItemId(int position) { return getItem(position).getId(); }
@Override public int getItemViewType(int position) { lastItemForViewTypeLookup = getItem(position); if (lastItemForViewTypeLookup == null) throw new RuntimeException("Invalid position " + position); return lastItemForViewTypeLookup.getLayout(); }
private void openLine(GroupAdapter adapter, int position) { Object item; while (--position >= 0) { item = adapter.getItem(position); if (item instanceof CommitFileHeaderItem) { openFile(((CommitFileHeaderItem) item).getFile()); return; } } }
@Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { Item item = groupAdapter.getItem(viewHolder.getAdapterPosition()); // Change notification to the adapter happens automatically when the section is // changed. swipeSection.remove(item); } };
item = adapter.getItem(position);