use of com.mikepenz.fastadapter.expandable.ExpandableExtension in project Hentoid by avluis.
the class ViewerGalleryFragment method updateListAdapter.
private void updateListAdapter(boolean isChapterEditMode) {
if (isChapterEditMode) {
if (!fastAdapter2.hasObservers())
fastAdapter2.setHasStableIds(true);
itemAdapter2.clear();
ExpandableExtension<INestedItem<SubExpandableItem.ViewHolder>> expandableExtension = fastAdapter2.getOrCreateExtension(ExpandableExtension.class);
GridLayoutManager glm = (GridLayoutManager) recyclerView.getLayoutManager();
if (glm != null) {
int spanCount = Preferences.getViewerGalleryColumns();
glm.setSpanCount(spanCount);
// Use the correct size to display chapter separators, if any
GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (fastAdapter2.getItemViewType(position) == R.id.expandable_item) {
return spanCount;
}
return 1;
}
};
glm.setSpanSizeLookup(spanSizeLookup);
}
// Activate drag & drop
SimpleDragCallback dragCallback = new SimpleDragCallback(this);
dragCallback.setNotifyAllDrops(true);
touchHelper = new ItemTouchHelper(dragCallback);
touchHelper.attachToRecyclerView(recyclerView);
recyclerView.setAdapter(fastAdapter2);
fastAdapter2.addEventHook(new SubExpandableItem.DragHandlerTouchEvent(position -> {
RecyclerView.ViewHolder vh = recyclerView.findViewHolderForAdapterPosition(position);
if (vh != null)
touchHelper.startDrag(vh);
return null;
}));
// Item click listener
fastAdapter2.setOnClickListener((v, a, i, p) -> onNestedItemClick(i));
// Select on swipe
if (mDragSelectTouchListener != null) {
recyclerView.removeOnItemTouchListener(mDragSelectTouchListener);
mDragSelectTouchListener = null;
}
} else {
if (!fastAdapter.hasObservers())
fastAdapter.setHasStableIds(true);
itemAdapter.clear();
// Gets (or creates and attaches if not yet existing) the extension from the given `FastAdapter`
selectExtension = fastAdapter.getOrCreateExtension(SelectExtension.class);
if (selectExtension != null) {
selectExtension.setSelectable(true);
selectExtension.setMultiSelect(true);
selectExtension.setSelectOnLongClick(true);
selectExtension.setSelectWithItemUpdate(true);
selectExtension.setSelectionListener((i, b) -> this.onSelectionChanged());
FastAdapterPreClickSelectHelper<ImageFileItem> helper = new FastAdapterPreClickSelectHelper<>(selectExtension);
fastAdapter.setOnPreClickListener(helper::onPreClickListener);
fastAdapter.setOnPreLongClickListener((v, a, i, p) -> {
// Warning : specific code for drag selection
mDragSelectTouchListener.startDragSelection(p);
return helper.onPreLongClickListener(v, a, i, p);
});
}
// Item click listener
fastAdapter.setOnClickListener((v, a, i, p) -> onItemClick(i));
GridLayoutManager glm = (GridLayoutManager) recyclerView.getLayoutManager();
if (glm != null) {
int spanCount = Preferences.getViewerGalleryColumns();
glm.setSpanCount(spanCount);
// Use the correct size to display chapter separators, if any
GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (fastAdapter.getItemViewType(position) == R.id.expandable_item) {
return spanCount;
}
return 1;
}
};
glm.setSpanSizeLookup(spanSizeLookup);
}
if (touchHelper != null)
touchHelper.attachToRecyclerView(null);
recyclerView.setAdapter(fastAdapter);
// Select on swipe
DragSelectTouchListener.OnDragSelectListener onDragSelectionListener = (start, end, isSelected) -> selectExtension.select(IntStream.rangeClosed(start, end).boxed().toList());
mDragSelectTouchListener = new DragSelectTouchListener().withSelectListener(onDragSelectionListener);
if (mDragSelectTouchListener2 != null) {
recyclerView.removeOnItemTouchListener(mDragSelectTouchListener2);
mDragSelectTouchListener2 = null;
}
recyclerView.addOnItemTouchListener(mDragSelectTouchListener);
}
}
Aggregations