0

How to change/remove item drag highlight? I'm using ItemTouchHelper and I know I need to do something with onChildDraw method but I don't want to override the whole ACTION_STATE_DRAG event, only need to affect that gray frame that appears when dragging items

What I have: gray frame

Edit: I noticed that the frame doesn't appear on pre Lollipop devices, so maybe I just need to set up some theme accent colors, but I can't find right selector for it (tried colorControlActivated and colorControlHighlight)

Hahaido
  • 83
  • 1
  • 11

3 Answers3

3

I solved my problem. That gray frame was just an element's elevation, ie simple shadow which indicates dragging (when you pick up an element to drag, it becomes above others, so it has a shadow; I was thinking that way and was right). So I set viewHolder.itemView.setOutlineProvider(null); and got rid of it. Complete code here:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setNullOutlineProvider(View view) {
    view.setOutlineProvider(null);
}

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, 
    RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, 
    boolean isCurrentlyActive) {

    final View view = viewHolder.itemView;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
        setNullOutlineProvider(view);

    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        // do some stuff here
    } else {
          super.onChildDraw(c, recyclerView, viewHolder, 
              dX, dY, actionState, isCurrentlyActive);
    }
}

Or better do it in your custom adapter

Hahaido
  • 83
  • 1
  • 11
1

overwrite onChildDrawOver() instead of onChildDraw(), onChildDraw() is for drawing background, onChildDrawOver() is for drawing foreground, try these code:

@Override
public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

    if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
        View itemView = viewHolder.itemView;
        c.save();
        c.clipRect(itemView.getLeft() + dX, itemView.getTop() + dY, itemView.getRight() + dX, itemView.getBottom() + dY);
        c.translate(itemView.getLeft() + dX, itemView.getTop() + dY);

        // draw the frame
        c.drawColor(0x88888888);

        c.restore();
    }
}
Harlan
  • 847
  • 5
  • 15
  • As you can see on my example, the frame is drawing below the view, so I don't need to use onChildDrawOver(). When I tried your code the gray frame is still there but the view is tinted with gray from onChildDrawOver() – Hahaido Jul 13 '16 at 07:18
  • I still can't realize what is the gray frame in your pic. Do you mean the shadow under the dialog? or you want to make the view brighter – Harlan Jul 13 '16 at 07:39
  • My example is representing the actual picture, not that I want to achieve. Upper dialog - is the normal state, lower dialog - the view is in drag state. At the left side of the lower dialog you could see a small part of ellipse gray stroke - that is the gray frame I'm talking about and I want to get rid of it – Hahaido Jul 13 '16 at 07:58
0

highlight was added by ItemTouchHelper.onChildDraw -> ItemTouchUIUtilImpl.onDraw
override it works for me

//ItemTouchUIUtilImpl.onDraw
public void onDraw(Canvas c, RecyclerView recyclerView, View view, float dX, float dY,
        int actionState, boolean isCurrentlyActive) {
    if (Build.VERSION.SDK_INT >= 21) {
        if (isCurrentlyActive) {
            Object originalElevation = view.getTag(R.id.item_touch_helper_previous_elevation);
            if (originalElevation == null) {
                originalElevation = ViewCompat.getElevation(view);
                float newElevation = 1f + findMaxElevation(recyclerView, view);
                ViewCompat.setElevation(view, newElevation);
                view.setTag(R.id.item_touch_helper_previous_elevation, originalElevation);
            }
        }
    }

    view.setTranslationX(dX);
    view.setTranslationY(dY);
}


//subclass of ItemTouchHelper.Callback
override fun onChildDraw(
    c: Canvas,
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder,
    dX: Float,
    dY: Float,
    actionState: Int,
    isCurrentlyActive: Boolean
) {
    viewHolder.itemView.setTranslationX(dX)
    viewHolder.itemView.setTranslationY(dY)
}
Lewis Weng
  • 942
  • 7
  • 5