justasm / DragLinearLayout

Android LinearLayout with drag and drop to reorder.
MIT License
453 stars 121 forks source link

bugfix: DragLinearLayout animation conflicts with LayoutTransition #8

Closed xuxucode closed 9 years ago

xuxucode commented 9 years ago

An exception is thrown when add android:animateLayoutChanges="true" to DragLinearLayout:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
            at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
            at android.view.ViewGroup.addView(ViewGroup.java:3415)
            at android.view.ViewGroup.addView(ViewGroup.java:3360)
            at com.jmedeisis.draglinearlayout.DragLinearLayout.onDrag(DragLinearLayout.java:447)
            at com.jmedeisis.draglinearlayout.DragLinearLayout.onTouchEvent(DragLinearLayout.java:661)
            at android.view.View.dispatchTouchEvent(View.java:7714)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
justasm commented 9 years ago

Thanks again!

justasm commented 9 years ago

I hadn't tested this scenario, so good catch.

I would think that we can re-enable the LayoutTransition immediately upon onDragStop() and not at the end of the settle animation though, as no further View removal / addition happens once the item is settling. Doesn't make a big difference either way as layout changes should be relatively rare.

xuxucode commented 9 years ago

Thanks for your advise, I enabled LayoutTransition immediately upon onDragStop() (see gist), but an unexpected blink occurs at the end of settle animation, so re-enabling LayoutTransition at animation end could do the trick.