我的要求是:-
edittext 值应该是连续的 例如:0:05、0:10、1:12,等等
一切都已完成,但可以说在位置 1 我更改为 0:05,因此在位置我可以显示错误,但在移动到 0 并将其修复为 0:00 之后,我如何更新/通知下一个位置的错误
通知下一个位置后,第三个位置也收到与第二个位置相同的值通知
所以值现在是 0:00 、 0:05(显示错误)、0:05(显示错误)
代码:-
inner class ViewHolder(view: ItemBinding) :
BaseRecyclerViewHolder<AddModel>(view.root) {
override fun bind(item: AddModel) {
val position = absoluteAdapterPosition
binding.apply {
if (item.time.isNotEmpty()) {
edtTime.apply {
text = Editable.Factory.getInstance().newEditable(item.time)
alpha = 1F
}
}
if (item.title.isNotEmpty()) {
edtTitle.apply {
text = Editable.Factory.getInstance().newEditable(item.title)
alpha = 1F
}
}
if (item.errorMessage.isNotEmpty()) {
txtErrorMessage.visible()
txtErrorMessage.text = item.errorMessage
} else {
txtErrorMessage.gone()
}
edtTime.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun afterTextChanged(editable: Editable) {
edtTime.alpha = if (editable.isNotEmpty()) 1F else 0.7F
errorTextView = txtErrorMessage
editTextTime = edtTime
txtErrorMessage.gone()
currentValue = editable.toString()
item.time = currentValue
if (!isValidTimeFormat(editable.toString())) {
showError("Invalid Format", true, position)
onItemClick.invoke(
ItemClickEnum.ENABLEDISABLEBUTTON,
item,
position
)
return
}
val timeParts = editable.split(":")
if (timeParts.size != 2) {
return
}
val minutes = timeParts[0].toIntOrNull() ?: return
val seconds = timeParts[1].toIntOrNull() ?: return
val totalSeconds = minutes * 60 + seconds
if (totalSeconds < 5 && position != 0) {
showError(
"Each chapter should be min 5 sec",
true,
position
)
return
}
if (totalSeconds > videoDurationInMillis / 1000) {
showError(
"Time exceeded the video duration",
true,
position
)
return
}
when (position) {
0 -> handlePositionZero(totalSeconds, position)
items.size - 1 -> handleLastPosition(totalSeconds, position)
else -> handleInBetweenPosition(totalSeconds, position)
}
onItemClick.invoke(
ItemClickEnum.ENABLEDISABLEBUTTON,
item,
position
)
}
})
}
}
fun isValidTimeFormat(input: String): Boolean {
val regexPattern = """^([0-9]+):[0-5]\d$""".toRegex()//"""^(\d{2,3}):[0-5]\d$""".toRegex()
return regexPattern.matches(input)
}
private fun showError(errorMessage: String, showError: Boolean, position: Int) {
errorTextView.text = errorMessage
if (showError) {
errorTextView.visible()
items[position].errorMessage = errorMessage
editTextTime.backgroundTintList =
ContextCompat.getColorStateList(editTextTime.context, R.color.kred)
editTextTime.setHintTextColor(
ContextCompat.getColor(editTextTime.context, R.color.kred)
)
editTextTime.setTextColor(
ContextCompat.getColor(editTextTime.context, R.color.kred)
)
} else {
errorTextView.gone()
items[position].errorMessage = errorMessage
editTextTime.backgroundTintList =
ContextCompat.getColorStateList(editTextTime.context, R.color.grey_border)
editTextTime.setHintTextColor(
ContextCompat.getColor(editTextTime.context, R.color.grey_border))
editTextTime.setTextColor(
ContextCompat.getColor(editTextTime.context, R.color.grey_border))
editTextTime.alpha = 1F
}
Executors.newSingleThreadScheduledExecutor().schedule({
// updateItemAt(position +1, items.get(position+1));
// notifyItemChanged(position+1)
notifyItemChanged(position+1)
}, 4, TimeUnit.SECONDS)
}
private fun handlePositionZero(totalSeconds: Int , position: Int) {
val nextPosition = position + 1
if (items[nextPosition].time.isNotEmpty()) {
val nextTime = parseTime(items[nextPosition].time)
val timeDifferenceWithNext = nextTime - totalSeconds
if (timeDifferenceWithNext < 5 || timeDifferenceWithNext > 300) {
showError("Please add sequential", true, position)
} else {
showError("", false, position)
}
} else {
showError("", false, position)
}
}
private fun handleLastPosition(totalSeconds: Int ,position: Int) {
val prevPosition = position - 1
if (items[prevPosition].time.isNotEmpty()) {
val prevTime = parseTime(items[prevPosition].time)
val timeDifferenceWithPrev = totalSeconds - prevTime
if (timeDifferenceWithPrev < 5 || timeDifferenceWithPrev > 300) {
showError("Please add sequential", true, position)
} else {
showError("", false, position)
}
} else {
showError("", false, position)
}
}
private fun handleInBetweenPosition(totalSeconds: Int ,position: Int) {
val prevTime = parseTime(items[position - 1].time)
val nextTime = parseTime(items[position + 1].time)
val timeDifferenceWithPrev = totalSeconds - prevTime
val timeDifferenceWithNext = nextTime - totalSeconds
if (items[position - 1].time.isNotEmpty() && items[position + 1].time.isNotEmpty()) {
if (timeDifferenceWithPrev > 300 || timeDifferenceWithNext > 300 || timeDifferenceWithPrev < 5 || timeDifferenceWithNext < 5) {
showError("Please add sequential", true, position)
} else {
showError("", false, position)
}
} else if (items[position - 1].time.isNotEmpty()) {
if (timeDifferenceWithPrev < 5 || timeDifferenceWithPrev > 300) {
showError("Please add sequential", true, position)
} else {
showError("", false, position)
}
} else if (items[position + 1].time.isNotEmpty()) {
if (timeDifferenceWithNext < 5 || timeDifferenceWithNext > 300) {
showError("Please add sequential", true, position)
} else {
showError("", false, position)
}
}
}
}
请帮助我如何通知上一个和下一个单元格保持当前状态并验证
提前致谢!