我已在recyclerView项上设置了滑动手势以进行编辑。我已经做到了:
private val swipeHelper = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
return false
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.adapterPosition //get position which is swipe
if (direction == ItemTouchHelper.RIGHT) {
… // do stuff
}
}
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
val icon: Bitmap
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
val itemView = viewHolder.itemView
val height: Float = (itemView.bottom.toFloat() - itemView.top.toFloat())
val width = height / 3
if (dX > 0) {
paint.color = Color.parseColor("#FF9300")
val background = RectF(itemView.left.toFloat(), itemView.top.toFloat(), dX, itemView.bottom.toFloat())
c.drawRect(background, paint)
icon = BitmapFactory.decodeResource(resources, R.drawable.pencil)
val iconDest = RectF(
itemView.left.toFloat() + width,
itemView.top.toFloat() + width,
itemView.left.toFloat() + 2*width,
itemView.bottom.toFloat() - width)
c.drawBitmap(icon, null, iconDest, paint)
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}
}
我面临的问题是,我无法弄清楚如何解决的问题是,在滑动之后,由于我没有删除元素,因此在完全重新启动活动之前,不会重置动画。
以下是说明图:
有人处理过这个问题吗?关于如何解决它而不必重新启动整个活动的任何想法?
答案 0 :(得分:1)
在适配器上呼叫notifyItemChanged
。这将重置适配器。
答案 1 :(得分:0)
您可以在活动中通过调用notifyDatasetChanged();
或notifyItemChanged();
来做到这一点:
ItemTouchHelper.SimpleCallback callback = new RecyclerItemTouchHelper(this, 0, ItemTouchHelper.LEFT
, new RecyclerItemTouchHelper.RecyclerItemTouchHelperListener() {
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, final int position) {
if (direction == ItemTouchHelper.LEFT) {
// do what you want here and after that call the function below:
recyclerAdapter.notifyItemChanged(position);
}
}
});
new ItemTouchHelper(callback).attachToRecyclerView(recyclerView);