RecyclerView onSwiped无法按计划运行

时间:2016-04-29 02:11:31

标签: android android-recyclerview

在从here添加自定义视图时,我注意到了一些问题。

  • 由于项目不清楚,当移出屏幕时,它会留下一个间隙孔。
  • 滑动但不完全时,屏幕上仍会显示警报图标。

这是我的困境的截图: bugs bugs everywhere

ItemTouchHelperCallback:

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    View itemView = viewHolder.itemView;
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        itemView = viewHolder.itemView;
        float height = (float) itemView.getBottom() - (float) itemView.getTop();
        float width = height / 3;

        // First, a rectangle is drawn on Canvas which covers the whole item view in preferred color.
        // Then, we draw a Bitmap icon over that in specified location.
        // Swiped right, green background with tick icon
        // Swiped left, Orange background with alarm clock.
        if(dX > 0){
            // On the left side i.e swiped right
            p.setColor(ContextCompat.getColor(context, R.color.green));

            // Draw Rect with varying right side, equal to displacement dX
            RectF background = new RectF((float) itemView.getLeft(), (float) itemView.getTop(), dX,(float) itemView.getBottom());
            c.drawRect(background,p);

            // Retrieves icon as Bitmap
            icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_tick);
            RectF icon_dest = new RectF((float) itemView.getLeft() + width ,(float) itemView.getTop() + width,(float) itemView.getLeft()+ 2*width,(float)itemView.getBottom() - width);

            // Set the image icon for Right swipe
            c.drawBitmap(icon,null,icon_dest,p);
        } else {
            // On the right side i.e swiped left
            p.setColor(ContextCompat.getColor(context, R.color.orange));

            // Draw Rect with varying left side, equal to the item's right side
            // plus negative displacement dX
            RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(),(float) itemView.getRight(), (float) itemView.getBottom());
            c.drawRect(background,p);

            // Retrieves icon as Bitmap
            icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_alarm_off);
            RectF icon_dest = new RectF((float) itemView.getRight() - 2*width ,(float) itemView.getTop() + width,(float) itemView.getRight() - width,(float)itemView.getBottom() - width);

            // Set the image icon for Left swipe
            c.drawBitmap(icon,null,icon_dest,p);
        }

        // Fade out the view as it is swiped out of the parent's bounds
        final float alpha = 1.0f - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
        viewHolder.itemView.setAlpha(alpha);
        viewHolder.itemView.setTranslationX(dX);
    }
    else 
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder,
                              int actionState) {
    // We only want the active item
    if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
        if (viewHolder instanceof ItemTouchHelperViewHolder) {
            ItemTouchHelperViewHolder itemViewHolder =
                    (ItemTouchHelperViewHolder) viewHolder;
            itemViewHolder.onItemSelected();
            viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
        }
    }

    super.onSelectedChanged(viewHolder, actionState);
}

@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    // Called when RV item goes back into idle state i.e. finished drag or swipe
    super.clearView(recyclerView, viewHolder);
    if(viewHolder instanceof ItemTouchHelperViewHolder) {
        ItemTouchHelperViewHolder itemTouchHelperViewHolder = (ItemTouchHelperViewHolder) viewHolder;
        itemTouchHelperViewHolder.onItemClear();
        viewHolder.itemView.setBackgroundColor(0);
        icon.recycle();
    }
}

我将如何:

  1. 使位图淡出,以便下面的项目更新以填补空白

  2. 修复图标和空间问题

  3. 解决我的问题并降低血压

  4. 如果您能提供任何解决方案,我将不胜感激。

    更新

    我修复图标问题的方式非常愚蠢。在else之后的if (dX > 0)语句中,我替换为else if (dX < 0),因此图标消失了。我的理论是,这是因为当替换RV项目时,dX不是> 0,因此调用else语句再次绘制图标。

1 个答案:

答案 0 :(得分:0)

我修复图标问题的方式非常愚蠢。在else之后的if (dX > 0)语句中,我替换为else if (dX < 0),因此图标消失了。我的理论是,这是因为当替换RV项目时,dX不是> 0,因此调用else中的onChildDraw语句,再次绘制图标。