简体   繁体   English

在 RecyclerView 中滑动时不显示 Drawable

[英]Drawable not showing when swiping in RecyclerView

I am able to get a background color when swiping using this code:使用此代码滑动时,我可以获得背景颜色:

@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    ColorDrawable colorDrawable=new ColorDrawable();
                
    colorDrawable.setColor(getResources().getColor(R.color.white));
    colorDrawable.setBounds(viewHolder.itemView.getLeft() , viewHolder.itemView.getTop(), viewHolder.itemView.getRight(), 
    viewHolder.itemView.getBottom());
    colorDrawable.draw(c);        
}

But I am not able to get an icon while swiping:但是我在刷卡时无法获得图标:

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

    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    ColorDrawable colorDrawable=new ColorDrawable();
   
    colorDrawable.setColor(getResources().getColor(R.color.white));
    colorDrawable.setBounds(viewHolder.itemView.getLeft() , viewHolder.itemView.getTop(), viewHolder.itemView.getRight(), viewHolder.itemView.getBottom());
    colorDrawable.draw(c);

    Drawable bitmapDrawable= ResourcesCompat.getDrawable(getResources(), R.drawable.ic_edit_24, null);
                
    bitmapDrawable.setBounds(viewHolder.itemView.getLeft()/2, v viewHolder.itemView.getTopp(), viewHolder.itemView.getLeft()/2, viewHolder.itemView.getBottom());
    bitmapDrawable.draw(c);
}

What is the issue with my code?我的代码有什么问题?

You're using viewHolder.itemView.getLeft()/2 for both the left and right bound.您正在使用viewHolder.itemView.getLeft()/2左右边界。 Therefore it's effective width would be zero.因此它的有效宽度为零。

You're probably looking to use something like:您可能正在寻找使用类似的东西:

viewHolder.itemView.getLeft() / 2 - desiredDrawableWidth / 2
viewHolder.itemView.getLeft() / 2 + desiredDrawableWidth / 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM