0

如何更改/删除项目拖动突出显示?我正在使用ItemTouchHelper并且我知道我需要使用onChildDraw方法做一些事情,但我不想覆盖整个ACTION_STATE_DRAG事件,只需要影响拖动项目时出现的灰色框

我有什么: 灰色框架

编辑:我注意到框架没有出现在棒棒糖之前的设备上,所以也许我只需要设置一些主题强调色,但我找不到合适的选择器(试过colorControlActivatedcolorControlHighlight

4

3 回答 3

3

我解决了我的问题。那个灰色的框只是一个元素的高度,即简单的阴影,表示拖动(当你拿起一个要拖动的元素时,它会在其他元素之上,所以它有一个阴影;我是这样想的,是对的)。所以我设置viewHolder.itemView.setOutlineProvider(null);并摆脱了它。完整代码在这里:

@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);
    }
}

或者更好地在您的自定义适配器中进行

于 2016-07-13T11:51:11.263 回答
0

overwriteonChildDrawOver()而不是onChildDraw(),onChildDraw()用于绘制背景,onChildDrawOver()用于绘制前景,请尝试以下代码:

@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();
    }
}
于 2016-07-13T07:02:23.810 回答
0

突出显示由 ItemTouchHelper.onChildDraw 添加-> ItemTouchUIUtilImpl.onDraw
覆盖它对我有用

//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)
}
于 2021-08-16T09:12:30.600 回答