关联用户滑动

This commit is contained in:
2026-05-23 08:57:31 +08:00
parent f3c1ae70d4
commit b16ced1c18
3 changed files with 117 additions and 38 deletions

View File

@@ -16,6 +16,7 @@ data class LinkedUser(
class LinkedUserAdapter(
private val deleteText: String,
private val isUnlinkMode: Boolean,
private val onItemClick: (LinkedUser, Int) -> Unit,
private val onDeleteClick: (LinkedUser, Int) -> Unit
) : RecyclerView.Adapter<LinkedUserAdapter.LinkedUserViewHolder>() {
@@ -35,6 +36,12 @@ class LinkedUserAdapter(
}
}
fun closeSwipe(position: Int) {
if (position in items.indices) {
notifyItemChanged(position)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LinkedUserViewHolder {
val binding = ItemLinkedUserBinding.inflate(
LayoutInflater.from(parent.context),
@@ -56,18 +63,38 @@ class LinkedUserAdapter(
) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: LinkedUser, position: Int) {
binding.tvPhone.text ="手机号码:${item.phone}"
binding.tvIdCard.text ="身份证号:${item.idCard}"
binding.tvPhone.text = "手机号码:${item.phone}"
binding.tvIdCard.text = "身份证号:${item.idCard}"
binding.llContent.translationX = 0f
binding.tvUserName.text = item.name
binding.tvTime.text = item.time
//设置删除按钮的文字
binding.tvDeleteAction.text = deleteText
binding.llContent.setOnClickListener {
onItemClick(item, position)
// MODE_SETTINGS → 显示"解除关联"MODE_PERSONAL → 显示"删除"
if (isUnlinkMode) {
binding.tvUnlinkAction.visibility = View.VISIBLE
binding.tvDeleteAction.visibility = View.GONE
} else {
binding.tvUnlinkAction.visibility = View.GONE
binding.tvDeleteAction.visibility = View.VISIBLE
binding.tvDeleteAction.text = deleteText
}
binding.llDelete.setOnClickListener {
binding.llContent.setOnClickListener {
if (binding.llContent.translationX < 0f) {
binding.llContent.animate()
.translationX(0f)
.setDuration(200)
.start()
} else {
onItemClick(item, position)
}
}
binding.tvDeleteAction.setOnClickListener {
onDeleteClick(item, position)
}
binding.tvUnlinkAction.setOnClickListener {
onDeleteClick(item, position)
}
}

View File

@@ -2,7 +2,7 @@ package com.example.defenseapplication.liveVideo
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
@@ -73,8 +73,10 @@ class LinkedUserActivity : AppCompatActivity() {
private fun initView() {
val deleteText = if (mode == MODE_SETTINGS) getString(R.string.unlink) else getString(R.string.delete)
val isUnlinkMode = mode == MODE_SETTINGS
adapter = LinkedUserAdapter(
deleteText = deleteText,
isUnlinkMode = isUnlinkMode,
onItemClick = { user, position ->
val intent = Intent(this, UserDetailsActivity::class.java).apply {
putExtra("name", user.name)
@@ -91,9 +93,14 @@ class LinkedUserActivity : AppCompatActivity() {
binding.rvLinkedUser.layoutManager = LinearLayoutManager(this)
binding.rvLinkedUser.adapter = adapter
val swipeThreshold = -300f
// 根据模式只显示一个按钮,滑动露出完整按钮
val swipeThresholdPx = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, -100f, resources.displayMetrics
)
val heldPositions = mutableMapOf<Int, Boolean>()
var currentlyHeldPosition: Int = -1
val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
override fun onMove(
recyclerView: RecyclerView,
@@ -117,21 +124,53 @@ class LinkedUserActivity : AppCompatActivity() {
val contentLayout = viewHolder.getContentLayout()
val position = viewHolder.adapterPosition
// 如果当前滑动的位置和已展开的位置不同,先收起已展开的项
if (isCurrentlyActive && currentlyHeldPosition != -1 && currentlyHeldPosition != position) {
val oldViewHolder = recyclerView.findViewHolderForAdapterPosition(currentlyHeldPosition)
if (oldViewHolder is LinkedUserAdapter.LinkedUserViewHolder) {
oldViewHolder.getContentLayout().animate()
.translationX(0f)
.setDuration(200)
.start()
}
heldPositions.remove(currentlyHeldPosition)
currentlyHeldPosition = -1
}
if (isCurrentlyActive) {
val swipeDistance = if (dX < swipeThreshold) swipeThreshold else dX
contentLayout.translationX = swipeDistance
val isHeld = heldPositions[position]
if (isHeld == true) {
if (dX > 0) {
// 已展开时向右滑动 → 平滑收回
val newX = swipeThresholdPx + dX
// 不能超过原位
contentLayout.translationX = if (newX > 0) 0f else newX
} else {
// 保持展开或继续左滑(限制不超过阈值)
contentLayout.translationX = swipeThresholdPx
}
} else {
// 未展开,正常滑动限制(左滑不超过阈值,右滑不超过原位)
val swipeDistance = when {
dX < swipeThresholdPx -> swipeThresholdPx
dX > 0 -> 0f
else -> dX
}
contentLayout.translationX = swipeDistance
}
} else {
val isHeld = heldPositions[position]
if (isHeld == true) {
contentLayout.translationX = swipeThreshold
contentLayout.translationX = swipeThresholdPx
}
}
}
}
/*
- 滑动超过一半后松手:列表项 保持在滑动位置 ,显示"解除关联"按钮
- 滑动超过一半后松手:列表项 保持在滑动位置,显示按钮
- 滑动未超过一半松手:列表项 恢复原位
- 已展开状态下向右拖动归位或拖动超过一半松手:恢复原位
* */
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
super.clearView(recyclerView, viewHolder)
@@ -139,11 +178,16 @@ class LinkedUserActivity : AppCompatActivity() {
val position = viewHolder.adapterPosition
val contentLayout = viewHolder.getContentLayout()
if (contentLayout.translationX < swipeThreshold / 2) {
// 判断滑动距离是否超过一半阈值(注意都是负数,所以绝对值比较)
if (Math.abs(contentLayout.translationX) > Math.abs(swipeThresholdPx / 2)) {
heldPositions[position] = true
contentLayout.translationX = swipeThreshold
currentlyHeldPosition = position
contentLayout.translationX = swipeThresholdPx
} else {
heldPositions.remove(position)
if (currentlyHeldPosition == position) {
currentlyHeldPosition = -1
}
contentLayout.animate()
.translationX(0f)
.setDuration(300)