Wednesday, March 15, 2017

ItemTouchHelper make dragged item go over fragment footer

Leave a Comment

I have a RecyclerView with an ItemTouchHelper. The RecyclerView is a 2/2 grid and it has a header and footer on the page. When the user picks up an item I want that item to be able to hover over the footer. However, I do not want the other items in the recycler view to be able to be seen through the footer.

So first I tried adding clipChildren and clipPadding but that created a situation where the RecyclerView items were scrolling up through the footer which was a problem. I tried adding elevation to the item and bringToFont but those never worked because the recycler view was still behind the footer, I tried adding bring to front to the recycler view itself but then once again all the items become in front of the footer again instead of just the dragged one, does anyone have any other ideas on how to solve this?

2 Answers

Answers 1

Suggestion, in my custom TouchHelperCallback I overrided onMove method and ignored checking if variables source and type are the same. So when I was dragging item over header, I could put item over it.

@Override     public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {         //if (source.getItemViewType() != target.getItemViewType()) {         //    return false;         //}          // Notify the adapter of the move         mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());         return true;     } 

Not modified tutorial about drag drop I took from: https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf#.jl59rui0l

Answers 2

I would start by making sure your recycler view has android:clipChildren="false". That way you should not need the recyclerview to be literally overlapped by the footer - it can end above the footer and still allow it's children to draw outside of bounds.

If you have a static footer you can try calling bringToFront when you determine y axis has moved far enough that your current item intersects the footer. Then when you move back out of range or when the recyclerview scrolls at all, bring the footer back to the front. However this would be particularly error prone; a bit of a hack.

Edit:

A different approach would be to completely sever the connection between a dragged item and the recyclerview (this will take a bit of work on the drop part), the steps would roughly be:

- Start dragging item i - store a reference to i.getParent() and the view index within its parent - i.getParent().removeView(i) //detach the item from the recycler (or create clone) - layout.addView(i) //add item onto main layout containing footer, allowing overlap  - when dropped, use the parent reference and index to reattach the item - then work out where the item was released 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment