我正在寻找一种功能,用于在“回收者”视图中滑动物品,当物品到达特定点时,它应该自动向后滑动。
我正在使用ItemTouchHelper.SimpleCallback
,下面是我尝试达到的目的。
1的代码段
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float,
actionState: Int, isCurrentlyActive: Boolean) {
setSwipeView(viewHolder)
val dx: Float
val isActive: Boolean
val screenWidth = Resources.getSystem().displayMetrics.widthPixels
if (dX <= 0) {
bottomTextView?.gravity = Gravity.CENTER or Gravity.END
context?.color(R.color.bloodRed)?.let { bottomTextView?.setBackgroundColor(it) }
dx = dX
isActive = false
} else {
bottomTextView?.gravity = Gravity.CENTER or Gravity.START
context?.color(R.color.dark_gray)?.let { bottomTextView?.setBackgroundColor(it) }
dx = if (dX > (screenWidth * 0.8)) {
isActive = false
0f
} else {
isActive = true
dX
}
}
getDefaultUIUtil().onDraw(c, recyclerView, topView, dX/4, dY,
actionState, isActive)
}
2的代码段
override fun onChildDrawOver(c: Canvas, recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float,
actionState: Int, isCurrentlyActive: Boolean) {
setSwipeView(viewHolder)
var dx = dX
if (dX <= 0) {
bottomTextView?.gravity = Gravity.CENTER or Gravity.END
context?.color(R.color.bloodRed)?.let { bottomTextView?.setBackgroundColor(it) }
} else {
dx = (dX * 0.8).toFloat()
bottomTextView?.gravity = Gravity.CENTER or Gravity.START
context?.color(R.color.dark_gray)?.let { bottomTextView?.setBackgroundColor(it) }
}
getDefaultUIUtil().onDraw(c, recyclerView, topView, dx, dY,
actionState, isCurrentlyActive)
}
在活动中
searchResultAdapter.notifyItemChanged(viewHolder.adapterPosition)
我也尝试了此(https://stackoverflow.com/a/38244462/6625633)解决方案,但它仅在第一次使用,并且该项目无法滚动之后。