Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable dragging for some cells #4

Closed
pixelbendr opened this issue Jul 2, 2015 · 3 comments
Closed

disable dragging for some cells #4

pixelbendr opened this issue Jul 2, 2015 · 3 comments
Labels

Comments

@pixelbendr
Copy link

Hello Paul,

so in my app I want users to be able to delete cells that they created. so for example in a group chat you can only delete a message you create. so swipe to dismiss should be disabled for messages which the user didn't create.

Whats the best way to approach this?

@Shusshu
Copy link

Shusshu commented Jul 13, 2015

In "SimpleItemTouchHelperCallback" you can return false in the "isLongPressDragEnabled" method

@iPaulPro
Copy link
Owner

Sorry @pixelbendr, I seem to have missed the notification for this issue.

You'll notice that getMovementFlags provides a RecyclerView.ViewHolder parameter. This means that it's called for every item in the Adapter, individually. If you want to disable movement for a specific item, you would return 0 for that ViewHolder. Eg.

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
    final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
    return viewHolder.getItemViewType() == YourAdapter.Type.HEADER ? 0 : makeMovementFlags(dragFlags, swipeFlags);
}

@iPaulPro
Copy link
Owner

Note: You'll also have to add a type check in onMove. Eg

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
    // Restrict movement to source view type
    if (source.getItemViewType() != target.getItemViewType()) {
        return false;
    }

    // ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants