@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); adapter.add(mainSection); }
public @NonNull Item getItem(int position) { return getItem(groups, position); }
public void add(@NonNull Group group) { if (group == null) throw new RuntimeException("Group cannot be null"); int itemCountBeforeGroup = getItemCount(); group.registerGroupDataObserver(this); groups.add(group); notifyItemRangeInserted(itemCountBeforeGroup, group.getItemCount()); }
@Override public void onItemMoved(@NonNull Group group, int fromPosition, int toPosition) { int groupAdapterPosition = getAdapterPosition(group); notifyItemMoved(groupAdapterPosition + fromPosition, groupAdapterPosition + toPosition); }
@Override public void onItemRangeChanged(@NonNull Group group, int positionStart, int itemCount) { notifyItemRangeChanged(getAdapterPosition(group) + positionStart, itemCount); }
@NonNull @Override public Dialog onCreateDialog(final Bundle savedInstanceState) { int selected = getArguments().getInt(ARG_SELECTED_CHOICE); GroupAdapter adapter = new GroupAdapter(); for (GitReference ref : getChoices()) { adapter.add(new RefDialogItem(ref, selected)); } adapter.setOnItemClickListener(this); return createDialogBuilder() .adapter(adapter, null) .negativeText(R.string.cancel) .build(); }
rainbow500 = getResources().getIntArray(R.array.rainbow_500); groupAdapter = new GroupAdapter(); groupAdapter.setOnItemClickListener(onItemClickListener); groupAdapter.setOnItemLongClickListener(onItemLongClickListener); groupAdapter.setSpanCount(12); populateAdapter(); layoutManager = new GridLayoutManager(this, groupAdapter.getSpanCount()); layoutManager.setSpanSizeLookup(groupAdapter.getSpanSizeLookup());
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle args = getArguments(); base = args.getString(EXTRA_BASE); repository = args.getParcelable(EXTRA_REPOSITORY); mainSection.add(commitSection); mainSection.add(filesSection); mainSection.add(commentSection); adapter.add(mainSection); adapter.setOnItemClickListener(this); }
private Group makeCarouselGroup() { CarouselItemDecoration carouselDecoration = new CarouselItemDecoration(gray, betweenPadding); GroupAdapter carouselAdapter = new GroupAdapter(); for (int i = 0; i < 10; i++) { carouselAdapter.add(new CarouselCardItem(rainbow200[i])); } return new CarouselGroup(carouselDecoration, carouselAdapter); }
@Test public void addGroupToNestedSectionNotifiesAtCorrectIndex() throws Exception { final Section rootSection = new Section(); rootSection.registerGroupDataObserver(groupAdapter); groupAdapter.add(rootSection); final Section nestedSection1 = new Section(Arrays.asList(new DummyItem(), new DummyItem(), new DummyItem())); rootSection.add(nestedSection1); final Section nestedSection2 = new Section(Arrays.asList(new DummyItem(), new DummyItem())); reset(groupAdapter); rootSection.add(nestedSection2); verify(groupAdapter).onItemRangeInserted(rootSection, 3, 2); }
@Test public void removeAllUnorderedGroupsFromNestedSectionNotifiesAtCorrectIndexes() throws Exception { final Section rootSection = new Section(); rootSection.registerGroupDataObserver(groupAdapter); groupAdapter.add(rootSection); final Section nestedSection1 = new Section(Arrays.asList(new DummyItem(), new DummyItem(), new DummyItem())); rootSection.add(nestedSection1); final Section nestedSection2 = new Section(Collections.singletonList(new DummyItem())); rootSection.add(nestedSection2); final Section nestedSection3 = new Section(Arrays.asList(new DummyItem(), new DummyItem())); rootSection.add(nestedSection3); reset(groupAdapter); rootSection.removeAll(Arrays.asList(nestedSection2, nestedSection3, nestedSection1)); final InOrder adapterCalls = inOrder(groupAdapter, groupAdapter, groupAdapter); adapterCalls.verify(groupAdapter).onItemRangeRemoved(rootSection, 3, 1); adapterCalls.verify(groupAdapter).onItemRangeRemoved(rootSection, 3, 2); adapterCalls.verify(groupAdapter).onItemRangeRemoved(rootSection, 0, 3); }
/** * 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 onItemClick(Item item, View view) { adapter.remove(item); } }
@Override public void onItemClick(@NonNull Item item, @NonNull View view) { if (item instanceof GistFileItem) { int position = adapter.getAdapterPosition(item); startActivity(GistFilesViewActivity.createIntent(gist, position - 1)); } }
/** * Configure list after view has been created. * * @param recyclerView */ protected void configureList(RecyclerView recyclerView) { adapter.setOnItemClickListener(this); adapter.setOnItemLongClickListener(this); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); recyclerView.setAdapter(adapter); }
@Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { // This is pretty evil, try not to do this groupAdapter.notifyDataSetChanged(); } };
@Override public int getItemCount() { return getItemCount(groups); }
public CarouselGroup(RecyclerView.ItemDecoration itemDecoration, GroupAdapter adapter) { this.adapter = adapter; carouselItem = new CarouselItem(itemDecoration, adapter); isEmpty = adapter.getItemCount() == 0; adapter.registerAdapterDataObserver(adapterDataObserver); }
public void addAllWorksWithSets() { Set<Group> groupSet = new HashSet<>(); groupSet.add(new DummyItem()); groupSet.add(new DummyItem()); groupAdapter.addAll(groupSet); Assert.assertEquals(2, groupAdapter.getItemCount()); }