androidandroid-recyclerviewitemtouchhelper

RecyclerView drag highlight


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)


Solution

  • 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