Comments (8)
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.
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.
very helpful save my life,Thank you very much
from android-itemtouchhelper-demo.
@MFlisar you saved my day! Search for this kind of solution all day.. Thanks!
from android-itemtouchhelper-demo.
Works like a charm! Thanks @MFlisar
from android-itemtouchhelper-demo.
@MFlisar Thanks.
You saved my days of r&d...
from android-itemtouchhelper-demo.
what should be the code inside onDrop ?
from android-itemtouchhelper-demo.
thanks, working for me
from android-itemtouchhelper-demo.
Related Issues (20)
- Create lib and publish to Jcenter HOT 1
- Issue with getMoveThreshold() HOT 3
- ss
- Swipe to dismiss doesnt work on Grid layout manager?
- Change scrolling speed while drag-and-drop?
- How can we save the new list after drag? HOT 2
- swipe delete
- Single notification - onMove
- Click and drag images working together HOT 1
- How to resize the recyclerview layout with a header (grid)
- Position of item not getting changed when its moved
- Xàm lol
- Cannot Update the position of the dragged file HOT 1
- How to do drag and drop with different view types? HOT 3
- Sorts are not equal
- disable swipe
- How to get below item at the time of dragging ? not UP or Down
- Able to drag (not drop) outside of Recyclerview or add DragShadow
- not working in navigation drawer HOT 1
- Drag & Drop works slowly
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from android-itemtouchhelper-demo.