use of com.mikepenz.fastadapter.drag.SimpleDragCallback in project Hentoid by avluis.
the class DrawerEditActivity method onCreate.
@SuppressLint("NonConstantResourceId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_edit);
// Toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(v -> onBackPressed());
toolbar.setOnMenuItemClickListener(clickedMenuItem -> {
switch(clickedMenuItem.getItemId()) {
case R.id.action_check_all:
onCheckAll();
break;
case R.id.action_uncheck_all:
onUncheckAll();
break;
default:
}
return true;
});
// Activate drag & drop
SimpleDragCallback dragCallback = new SimpleDragCallback(this);
dragCallback.setNotifyAllDrops(true);
ItemTouchHelper touchHelper = new ItemTouchHelper(dragCallback);
// Recycler
List<SiteItem> items = new ArrayList<>();
List<Site> activeSites = Preferences.getActiveSites();
// First add active sites
for (Site s : activeSites) if (s.isVisible())
items.add(new SiteItem(s, true, touchHelper));
// Then add the others
for (Site s : Site.values()) // We don't want to show these
if (s.isVisible() && !activeSites.contains(s))
items.add(new SiteItem(s, false, touchHelper));
itemAdapter.add(items);
recyclerView = findViewById(R.id.drawer_edit_list);
recyclerView.setAdapter(fastAdapter);
recyclerView.setHasFixedSize(true);
touchHelper.attachToRecyclerView(recyclerView);
// OK button
View okBtn = findViewById(R.id.drawer_edit_ok_btn);
okBtn.setOnClickListener(v -> onValidateClick());
}
use of com.mikepenz.fastadapter.drag.SimpleDragCallback 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);
}
}
use of com.mikepenz.fastadapter.drag.SimpleDragCallback in project Hentoid by avluis.
the class MergeDialogFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View rootView, @Nullable Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
List<Content> contentList = loadContentList();
if (contentList.isEmpty())
return;
boolean isExternal = contentList.get(0).getStatus().equals(StatusContent.EXTERNAL);
itemAdapter.set(Stream.of(contentList).map(s -> new TextItem<>(s.getTitle(), s, true, false, false, touchHelper)).toList());
// Activate drag & drop
SimpleDragCallback dragCallback = new SimpleDragCallback(this);
dragCallback.setNotifyAllDrops(true);
touchHelper = new ItemTouchHelper(dragCallback);
touchHelper.attachToRecyclerView(binding.list);
fastAdapter.addEventHook(new TextItem.DragHandlerTouchEvent<>(position -> {
RecyclerView.ViewHolder vh = binding.list.findViewHolderForAdapterPosition(position);
if (vh != null)
touchHelper.startDrag(vh);
}));
binding.list.setAdapter(fastAdapter);
initialTitle = contentList.get(0).getTitle();
newTitleTxt = binding.titleNew.getEditText();
if (newTitleTxt != null)
newTitleTxt.setText(initialTitle);
if (isExternal) {
binding.mergeDeleteSwitch.setEnabled(Preferences.isDeleteExternalLibrary());
binding.mergeDeleteSwitch.setChecked(Preferences.isDeleteExternalLibrary() && deleteDefault);
} else {
binding.mergeDeleteSwitch.setEnabled(true);
binding.mergeDeleteSwitch.setChecked(deleteDefault);
}
binding.actionButton.setOnClickListener(v -> onActionClick());
}
use of com.mikepenz.fastadapter.drag.SimpleDragCallback in project Hentoid by avluis.
the class BookmarksDialogFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View rootView, @Nullable Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
MaterialButton homepage = requireViewById(rootView, R.id.bookmark_homepage_btn);
homepage.setIcon(ContextCompat.getDrawable(requireContext(), site.getIco()));
homepage.setOnClickListener(v -> parent.loadUrl(site.getUrl()));
List<SiteBookmark> bookmarks = reloadBookmarks();
// 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<TextItem<SiteBookmark>> helper = new FastAdapterPreClickSelectHelper<>(selectExtension);
fastAdapter.setOnPreClickListener(helper::onPreClickListener);
fastAdapter.setOnPreLongClickListener(helper::onPreLongClickListener);
}
recyclerView = requireViewById(rootView, R.id.bookmarks_list);
// Activate drag & drop
SimpleDragCallback dragCallback = new SimpleDragCallback(this);
dragCallback.setNotifyAllDrops(true);
touchHelper = new ItemTouchHelper(dragCallback);
touchHelper.attachToRecyclerView(recyclerView);
recyclerView.setAdapter(fastAdapter);
fastAdapter.setOnClickListener((v, a, i, p) -> onItemClick(i));
fastAdapter.addEventHook(new TextItem.DragHandlerTouchEvent<>(position -> {
RecyclerView.ViewHolder vh = recyclerView.findViewHolderForAdapterPosition(position);
if (vh != null)
touchHelper.startDrag(vh);
}));
selectionToolbar = requireViewById(rootView, R.id.toolbar);
selectionToolbar.setOnMenuItemClickListener(this::selectionToolbarOnItemClicked);
editMenu = selectionToolbar.getMenu().findItem(R.id.action_edit);
copyMenu = selectionToolbar.getMenu().findItem(R.id.action_copy);
homeMenu = selectionToolbar.getMenu().findItem(R.id.action_home);
bookmarkCurrentBtn = requireViewById(rootView, R.id.bookmark_current_btn);
Optional<SiteBookmark> currentBookmark = Stream.of(bookmarks).filter(b -> SiteBookmark.urlsAreSame(b.getUrl(), url)).findFirst();
if (currentBookmark.isPresent())
bookmarkId = currentBookmark.get().id;
updateBookmarkButton();
}
use of com.mikepenz.fastadapter.drag.SimpleDragCallback in project Hentoid by avluis.
the class LibraryGroupsFragment method setPagingMethod.
/**
* Initialize the paging method of the screen
*/
private void setPagingMethod() /*boolean isEditMode*/
{
viewModel.setPagingMethod(true);
itemAdapter = new ItemAdapter<>();
fastAdapter = FastAdapter.with(itemAdapter);
if (!fastAdapter.hasObservers())
fastAdapter.setHasStableIds(true);
// 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<GroupDisplayItem> helper = new FastAdapterPreClickSelectHelper<>(selectExtension);
fastAdapter.setOnPreClickListener(helper::onPreClickListener);
fastAdapter.setOnPreLongClickListener(helper::onPreLongClickListener);
}
// Drag, drop & swiping
if (activity.get().isEditMode()) {
SimpleDragCallback dragSwipeCallback = new SimpleSwipeDragCallback(this, this, ContextCompat.getDrawable(requireContext(), R.drawable.ic_action_delete_forever)).withSensitivity(10f).withSurfaceThreshold(0.75f);
dragSwipeCallback.setNotifyAllDrops(true);
// Despite its name, that's actually to disable drag on long tap
dragSwipeCallback.setIsDragEnabled(false);
touchHelper = new ItemTouchHelper(dragSwipeCallback);
touchHelper.attachToRecyclerView(recyclerView);
}
// Item click listener
fastAdapter.setOnClickListener((v, a, i, p) -> onItemClick(i));
// Favourite button click listener
fastAdapter.addEventHook(new ClickEventHook<GroupDisplayItem>() {
@Override
public void onClick(@NotNull View view, int i, @NotNull FastAdapter<GroupDisplayItem> fastAdapter, @NotNull GroupDisplayItem item) {
if (item.getGroup() != null)
onGroupFavouriteClick(item.getGroup());
}
@org.jetbrains.annotations.Nullable
@Override
public View onBind(RecyclerView.@NotNull ViewHolder viewHolder) {
if (viewHolder instanceof GroupDisplayItem.GroupViewHolder) {
return ((GroupDisplayItem.GroupViewHolder) viewHolder).getFavouriteButton();
}
return super.onBind(viewHolder);
}
});
fastAdapter.setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY);
recyclerView.setAdapter(fastAdapter);
recyclerView.setHasFixedSize(true);
}
Aggregations