现在我有一个功能性的向左滑动以在recyclerview中删除两个布局(前景和背景)。我在代码中使用了itemtouchhelper。但是,我想向左滑动并向右滑动以显示不同的颜色和图标,就像在谷歌收件箱中一样。我该如何实现呢?
我想要的是: swipe right swipe left
我拥有的是: only swipe right
代码只是标准的itemtouchhelper.simplecallback,在xml中有2个布局。我在各地搜索,只发现单个滑动选项,单个图标和单色
答案 0 :(得分:0)
使用 ItemTouchHelper 来实施类似Gmail的功能
在设置recyclerView
后调用以下函数import glob
import os
word="excel"
for (dirname, dirs, files) in os.walk("/batch/"):
for file_ in files :
if file_.startswith(word):
print(file_)
print(os.path.join(dirname, file_))
for dir_ in dirs :
myfiles = glob.glob(os.path.join(dirname,dir_))
for myfile in myfiles:
if myfile.startswith(word):
print(myfile)
print(os.path.join(dirname,myfiles))
其中
对象p是绘画对象private fun initSwipe() {
val simpleItemTouchCallback = 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
if (direction == ItemTouchHelper.LEFT) {
//Logic to do when swipe left
} else {
//Logic to do when swipe right
}
}
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) {
//Drawing for Swife Right
val itemView = viewHolder.itemView
val height = itemView.bottom.toFloat() - itemView.top.toFloat()
val width = height / 3
if (dX > 0) {
p.color = Color.parseColor("#2F2FD3")
val background = RectF(itemView.left.toFloat(), itemView.top.toFloat(), dX, itemView.bottom.toFloat())
c.drawRect(background, p)
icon = BitmapFactory.decodeResource(resources, R.drawable.ic_archive)
val icon_dest = RectF(itemView.left.toFloat() + width, itemView.top.toFloat() + width, itemView.left.toFloat() + 2 * width, itemView.bottom.toFloat() - width)
c.drawBitmap(icon, null, icon_dest, p)
} else {
//Drawing for Swife Left
p.color = Color.parseColor("#D32F2F")
val background = RectF(itemView.right.toFloat() + dX, itemView.top.toFloat(), itemView.right.toFloat(), itemView.bottom.toFloat())
c.drawRect(background, p)
icon = BitmapFactory.decodeResource(resources, R.drawable.ic_delete)
val icon_dest = RectF(itemView.right.toFloat() - 2 * width, itemView.top.toFloat() + width, itemView.right.toFloat() - width, itemView.bottom.toFloat() - width)
c.drawBitmap(icon, null, icon_dest, p)
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}
}
val itemTouchHelper = ItemTouchHelper(simpleItemTouchCallback)
itemTouchHelper.attachToRecyclerView(YOUR_RECYCLER_VIEW)
}
希望这可以帮到你。