Code Monkey home page Code Monkey logo

Comments (8)

MFlisar avatar MFlisar commented on May 26, 2024 13

Saving changes on every move is damn slow. But the docs of the ItemTouchHelper.Callback says, that it will be called if the drag is finished. So just do following (take notice of the mFrom and mTo and of the added function onDrop, you have to add this to the ItemTouchHelperAdapter interface):

Extend the SimpleItemTouchHelperCallback like following:

public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {

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

....

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
    if (source.getItemViewType() != target.getItemViewType()) {
        return false;
    }

    // remember FIRST from position
    if (mFrom == null)
        mFrom = source.getAdapterPosition();
    mTo = target.getAdapterPosition();

    // Notify the adapter of the move
    mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
    return true;
}

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

    viewHolder.itemView.setAlpha(ALPHA_FULL);

    if (viewHolder instanceof ItemTouchHelperViewHolder) {
        // Tell the view holder it's time to restore the idle state
        ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
        itemViewHolder.onItemClear();
    }

    if (mFrom != null && mTo != null)
        mAdapter.onDrop(mFrom, mTo);

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

from android-itemtouchhelper-demo.

kyleparker avatar kyleparker commented on May 26, 2024 1

Running into the same question/problem - after swiping/dismissing an item in the list, I need to perform actions in my top-level activity/fragment to update the DB, show the snackbar, etc. Looking forward to the next post and hopefully some insight on how to accomplish this!

Thank you for taking the time to write these posts - they are very helpful!

from android-itemtouchhelper-demo.

jishuo1213 avatar jishuo1213 commented on May 26, 2024

very helpful save my life,Thank you very much

from android-itemtouchhelper-demo.

matjazzeleznik avatar matjazzeleznik commented on May 26, 2024

@MFlisar you saved my day! Search for this kind of solution all day.. Thanks!

from android-itemtouchhelper-demo.

Gnzlt avatar Gnzlt commented on May 26, 2024

Works like a charm! Thanks @MFlisar

from android-itemtouchhelper-demo.

WasimMemon avatar WasimMemon commented on May 26, 2024

@MFlisar Thanks.
You saved my days of r&d...

from android-itemtouchhelper-demo.

priyankpandya1989 avatar priyankpandya1989 commented on May 26, 2024

what should be the code inside onDrop ?

from android-itemtouchhelper-demo.

AmitGunjan avatar AmitGunjan commented on May 26, 2024

thanks, working for me

from android-itemtouchhelper-demo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.