Comments (7)
I solve this by:
String tmp = mItems.get(fromPosition)
mItems.remove(fromPosition);
mItems.add(toPosition, tmp);
much shorter.
from android-itemtouchhelper-demo.
As mentioned in the article, onItemMove
is called every time an item index changes. This means that for your example, it will call
onItemMove(A, B)
then
onItemMove(B, C)
This is clearly demonstrated and working in the "Grid - Basic Swipe" example RecyclerGridFragment
.
from android-itemtouchhelper-demo.
Hello,
Here the proof it is not working as you say :
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
for (int i = 0; i < mItems.size(); i++) {
Log.d("DBG", "before item " + i + " = " + mItems.get(i));
}
Log.d("DBG", "move " + fromPosition + " to " + toPosition);
Collections.swap(mItems, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
for (int i = 0; i < mItems.size(); i++) {
Log.d("DBG", "after item " + i + " = " + mItems.get(i));
}
return true;
}
Before moving first item to the second line :
08-06 16:58:57.509: D/DBG(13376): before item 0 = One
08-06 16:58:57.509: D/DBG(13376): before item 1 = Two
08-06 16:58:57.509: D/DBG(13376): before item 2 = Three
08-06 16:58:57.509: D/DBG(13376): before item 3 = Four
08-06 16:58:57.509: D/DBG(13376): before item 4 = Five
08-06 16:58:57.509: D/DBG(13376): before item 5 = Six
08-06 16:58:57.509: D/DBG(13376): before item 6 = Seven
08-06 16:58:57.509: D/DBG(13376): before item 7 = Eight
08-06 16:58:57.509: D/DBG(13376): before item 8 = Nine
08-06 16:58:57.509: D/DBG(13376): before item 9 = Ten
08-06 17:02:39.719: D/DBG(13376): move 0 to 2
After :
08-06 17:02:39.719: D/DBG(13376): after item 0 = Three
08-06 17:02:39.719: D/DBG(13376): after item 1 = Two
08-06 17:02:39.719: D/DBG(13376): after item 2 = One
08-06 17:02:39.719: D/DBG(13376): after item 3 = Four
08-06 17:02:39.719: D/DBG(13376): after item 4 = Five
08-06 17:02:39.719: D/DBG(13376): after item 5 = Six
08-06 17:02:39.719: D/DBG(13376): after item 6 = Seven
08-06 17:02:39.719: D/DBG(13376): after item 7 = Eight
08-06 17:02:39.719: D/DBG(13376): after item 8 = Nine
08-06 17:02:39.719: D/DBG(13376): after item 9 = Ten
Which is wrong because the first item is "Two" and not "Three" :
from android-itemtouchhelper-demo.
onItemMove
would never be called from with 0 as fromPosition
and 2 as toPosition
. As I explained above, an actual drag from 0 to 2 would call
onItemMove(0,1)
onItemMove(1,2)
I have tested this on supported devices, and it works as expected. Are you actually performing a drag, or just debugging this with manual parameters?
from android-itemtouchhelper-demo.
Ah! You're saying that the items show properly, but the underlying data is not properly reflecting the change.
Sorry, I had misread your issue. Re-opening.
from android-itemtouchhelper-demo.
Yes it's all about mItems indexes
UI works like a charm but I'm saving in cache each item position for user and I needed data reflecting exactly the UI.
from android-itemtouchhelper-demo.
I will be updating the code and article to make this correction. Thanks!
Note, you can simplify to this:
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(mItems, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(mItems, i, i - 1);
}
}
notifyItemMoved(fromPosition, toPosition);
return true;
}
Since notifyItemMoved
takes care of both shifts (hence my error).
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.