question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GridLayout with Sections: GetSectionIndex return wrong value when dragging upwards

See original GitHub issue

Using a Gridlayout with sections that seperate your items (best represented by the “headers and sections example”):

While you drag, and get the value of getSectionIndex(draggedItemPosition) in shouldMove(from,to) ->getSectionIndex will return the wrong index if dragged from screenBottom direction to screenTop direction

Let’s say you have the following situation, 1 item, 1 header/section: [ item1 ] <- “area1” ------Header----- [ item2 ] <- “area2”

Correct behavior from screenTop direction to screenBottom (down):

  1. You drag item1 to area2
  2. in the called method shouldMove you call and print getSectionIndex(toPosition)
  3. You will get 0 (which is fine)

— Starting again with the normal situation shown above — Wrong behavior from screenBottom direction to screenTop direction (up):

  1. You drag item2 to area1
  2. in the called method shouldMove you call and print getSectionIndex(toPosition)
  3. You will get 1 —> which is not correct since the target position does not have any headers before it anymore

If you check after dragging getSectionIndex(item1Position) again -> you see that it’s correct (it’s 0) So while dragging the index is not calculated correctly (it does not consider the header that just moved above the dragged item)

As with the last bug, I am using your library extensively, so I am quite happy over quick feedback or even a fix. Thanks in advance

I need this to work correctly in order to prohibit the dragging of items above a particular header. i want to limit the range in which the user can drag items by two headers (above and below) . Between these two border headers are a lot of sections/headers where the user can organize stuff. If you have an alternative solution to get my intended behavior (in order to avoid the bug), I am also open to that alternatively I hope you understand what I mean. I tried to make my description as accurate and easy to understand as possible

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
davideascommented, Jul 30, 2016

@maltebucksch OK there’s no code, but what I said remains true: the item is still not moved and getSectionIndex(toPosition) cannot know that you are dragging(!): in toPosition there’s still the header so it is counted in. You have to adapt your use case with a super simple if statement:

@Override
public boolean shouldMove(int fromPosition, int toPosition) {
        Log.d(TAG, "Section-from: " + getSectionIndex(fromPosition) +
                   " Section-to: " + getSectionIndex((fromPosition > toPosition ? toPosition - 1 : toPosition));
        return onModuleShouldMoveListener == null || onModuleShouldMoveListener.shouldMove(fromPosition, toPosition);
}

With beta8 I can activate the shouldMove for the listener, I see that you use it.

0reactions
davideascommented, Jul 31, 2016

I’ve published the new SNAPSHOT with the shouldMoveItem callback, so you will have to implement it. If you have others callbacks customized you can extend your interface with the Adapter interface OnItemMoveListener, so that you can continue to use it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

GridLayout (not GridView) how to stretch all children evenly
Here is a small example that uses LinearLayout subviews. (I used Space Views that takes up unused area and pushes the buttons into...
Read more >
GridLayout | Android Developers
During layout, GridLayout solves the constraints so as to return the unique solution to those constraints for which all variables are less-than-or-equal-to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found