Search in sources :

Example 1 with SimpleSwipeDragCallback

use of com.mikepenz.fastadapter.swipe_drag.SimpleSwipeDragCallback 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);
}
Also used : SimpleSwipeDragCallback(com.mikepenz.fastadapter.swipe_drag.SimpleSwipeDragCallback) SimpleDragCallback(com.mikepenz.fastadapter.drag.SimpleDragCallback) SelectExtension(com.mikepenz.fastadapter.select.SelectExtension) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) NotNull(org.jetbrains.annotations.NotNull) SuppressLint(android.annotation.SuppressLint) ItemTouchHelper(androidx.recyclerview.widget.ItemTouchHelper) GroupDisplayItem(me.devsaki.hentoid.viewholders.GroupDisplayItem) RecyclerView(androidx.recyclerview.widget.RecyclerView) FastAdapterPreClickSelectHelper(me.devsaki.hentoid.widget.FastAdapterPreClickSelectHelper)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 ItemTouchHelper (androidx.recyclerview.widget.ItemTouchHelper)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 SimpleDragCallback (com.mikepenz.fastadapter.drag.SimpleDragCallback)1 SelectExtension (com.mikepenz.fastadapter.select.SelectExtension)1 SimpleSwipeDragCallback (com.mikepenz.fastadapter.swipe_drag.SimpleSwipeDragCallback)1 GroupDisplayItem (me.devsaki.hentoid.viewholders.GroupDisplayItem)1 FastAdapterPreClickSelectHelper (me.devsaki.hentoid.widget.FastAdapterPreClickSelectHelper)1 NotNull (org.jetbrains.annotations.NotNull)1