承接上一篇
为侧滑删除添加动画效果
1.在自定义的callback中重写onChildDraw方法
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
//super之前,来个侧滑缩放,透明度变化
if(actionState ==ItemTouchHelper.ACTION_STATE_SWIPE ) {
float alpha =1-Math.abs(dX)/viewHolder.itemView.getWidth();
viewHolder.itemView.setAlpha(alpha);
viewHolder.itemView.setScaleX(alpha);
viewHolder.itemView.setScaleY(alpha);
//此时有bug,因为viewHolder复用,那些复用的条目透明度和缩放也就变为了0
if(alpha <= 0) {
viewHolder.itemView.setAlpha(1);
viewHolder.itemView.setScaleX(1);
viewHolder.itemView.setScaleY(1);
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
此时缩放和透明度的变化就完成了,效果图如下。