Code Monkey home page Code Monkey logo

Comments (7)

callmepeanut avatar callmepeanut commented on May 27, 2024 8

I solve this by:
String tmp = mItems.get(fromPosition)
mItems.remove(fromPosition);
mItems.add(toPosition, tmp);
much shorter.

from android-itemtouchhelper-demo.

iPaulPro avatar iPaulPro commented on May 27, 2024

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.

anthony-skr avatar anthony-skr commented on May 27, 2024

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" :

screenshot_2015-08-06-17-03-15

from android-itemtouchhelper-demo.

iPaulPro avatar iPaulPro commented on May 27, 2024

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.

iPaulPro avatar iPaulPro commented on May 27, 2024

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.

anthony-skr avatar anthony-skr commented on May 27, 2024

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.

iPaulPro avatar iPaulPro commented on May 27, 2024

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)

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.