0


android:RecyclerView交互动画(上下拖动,左右滑动删除),Android程序员必看

@Override

public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

//监听侧滑;1.删除数据,2.调用adapter.notifyItemRemoved(position)

mMoveCallback.onItemRemove(viewHolder.getAdapterPosition());

}

//改变选中的Item

@Override

public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {

//判断状态

if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {

viewHolder.itemView.findViewById(R.id.textview).setBackgroundColor(viewHolder.itemView.getContext().getResources().getColor(R.color.black));

}

super.onSelectedChanged(viewHolder, actionState);

}

//恢复改变选中的Item

@Override

public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {

//恢复

viewHolder.itemView.findViewById(R.id.textview).setBackgroundColor(viewHolder.itemView.getContext().getResources().getColor(R.color.colorPrimary));

//透明度动画

viewHolder.itemView.setAlpha(1);//1~0

//缩放动画

viewHolder.itemView.setScaleX(1);//1~0

viewHolder.itemView.setScaleY(1);//1~0

super.clearView(recyclerView, viewHolder);

}

//在拖拽的时候做效果

@Override

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

//dx 水平方向移动的增量(负:向左;正:向右)范围:0~View.getWidth

float alpha=1 - Math.abs(dX) / viewHolder.itemView.getWidth();

if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {

//透明度动画

viewHolder.itemView.setAlpha(alpha);//1~0

//缩放动画

viewHolder.itemView.setScaleX(alpha);//1~0

viewHolder.itemView.setScaleY(alpha);//1~0

}

// if (alpha==0){

// //透明度动画

// viewHolder.itemView.setAlpha(1);//1~0

// //缩放动画

// viewHolder.itemView.setScaleX(1);//1~0

// viewHolder.itemView.setScaleY(1);//1~0

// }

super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

}

}

  • MainAdapter

import android.content.Context;

import

标签: android 交互

本文转载自: https://blog.csdn.net/2401_84149694/article/details/137448442
版权归原作者 2401_84149694 所有, 如有侵权,请联系我们删除。

“android:RecyclerView交互动画(上下拖动,左右滑动删除),Android程序员必看”的评论:

还没有评论