FastAdapter icon indicating copy to clipboard operation
FastAdapter copied to clipboard

Is it possible to have just some Items Swipeable?

Open hsdmiranda opened this issue 10 years ago • 6 comments

I have many different types of items on my adapter, but someone them should not be swipeable. Is there a way to achieve that? Like the IDraggable?

hsdmiranda avatar Sep 17 '16 08:09 hsdmiranda

@hsdmiranda you will have to extend the SimpleDragCallback and overwrite the getDragDirs method. https://github.com/mikepenz/FastAdapter/blob/develop/library-extensions/src/main/java/com/mikepenz/fastadapter_extensions/drag/SimpleDragCallback.java#L84

There you will not only check for IDraggable but you will also check if this item is really dragable, if not you will return 0.

mikepenz avatar Sep 17 '16 08:09 mikepenz

@hsdmiranda Just wanted to ask, if you got it working.

Rainer-Lang avatar Oct 03 '16 08:10 Rainer-Lang

@Rainer-Lang I did, but it wasn't so simple like was described.

hsdmiranda avatar Oct 04 '16 06:10 hsdmiranda

@hsdmiranda I'm glad you did it. Could I ask for the solution? It's also nice to have here the solution if someone else is searching for the same. Thanks.

Rainer-Lang avatar Oct 04 '16 10:10 Rainer-Lang

Sorry for the delay. This is what i did.

This class also contains something for drag and drop. I created a callback to notify when the item is actually dropped.

`

public class ImprovedSwipeDragCallback extends SimpleSwipeDragCallback {

private Integer mFrom = null;
private Integer mTo = null;

private ImprovedItemTouchCallback callback;

public ImprovedSwipeDragCallback(ImprovedItemTouchCallback itemTouchCallback, SimpleSwipeCallback.ItemSwipeCallback itemSwipeCallback, Drawable leaveBehindDrawable, int swipeDirs, @ColorInt int bgColor) {
    super(itemTouchCallback, itemSwipeCallback, leaveBehindDrawable, swipeDirs, bgColor);
    this.callback = itemTouchCallback;
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    if (target.itemView.getTag() instanceof IDraggable) {
        if (((IDraggable) target.itemView.getTag()).isDraggable()) {
            if (mFrom == null) {
                mFrom = viewHolder.getAdapterPosition();
            }
            mTo = target.getAdapterPosition();

            return super.onMove(recyclerView, viewHolder, target);
        }
    }

    return false;
}

@Override
public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    if (recyclerView==null || viewHolder == null)
        return super.getSwipeDirs(recyclerView,viewHolder);
    if (viewHolder.itemView.getTag() instanceof IDraggable) {
        if (((IDraggable) viewHolder.itemView.getTag()).isDraggable()) {
            return super.getSwipeDirs(recyclerView,viewHolder);
        } else {
            return 0;
        }
    } else {
        return 0;
    }
}

@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    super.clearView(recyclerView, viewHolder);

    if (mFrom != null && mTo != null) {
        callback.onDrop(mFrom, mTo);
    }

    // clear saved positions
    mFrom = mTo = null;
}

} `

hsdmiranda avatar Oct 11 '16 08:10 hsdmiranda

@hsdmiranda Thanks. If you have a bit time, may I ask you to contribute to this great lib and make a sample?

Rainer-Lang avatar Oct 12 '16 09:10 Rainer-Lang