修复BUG,优化UI

This commit is contained in:
muqing 2024-02-11 18:27:24 +08:00
parent c219e92777
commit ba74f5fb98

View File

@ -2,6 +2,7 @@ package com.coldmint.rust.pro
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.Menu import android.view.Menu
@ -69,13 +70,20 @@ class SearchActivity : BaseActivity<ActivitySearchBinding>() {
} }
private fun additem(string: String) { private fun additem(string: String) {
val indexOf = list.indexOf(string) // 检查列表中是否已经存在该数据
if (indexOf != -1) { val existingIndex = list.indexOf(string)
list.remove(string) if (existingIndex != -1) {
adapter.notifyItemRemoved(indexOf) // 如果已存在,移动到列表最前面
} list.removeAt(existingIndex)
list.add(0, string)
adapter.notifyItemMoved(existingIndex, 0)
} else {
// 如果不存在,添加到列表最前面
list.add(0, string) list.add(0, string)
adapter.notifyItemInserted(0) adapter.notifyItemInserted(0)
}
// 滚动到顶部
// viewBinding.hotSearchView2.scrollToPosition(0)
// 限制历史记录数量为10 // 限制历史记录数量为10
if (list.size > 10) { if (list.size > 10) {
list.removeAt(list.lastIndex) list.removeAt(list.lastIndex)
@ -86,9 +94,9 @@ class SearchActivity : BaseActivity<ActivitySearchBinding>() {
} }
fun search(string: String) { fun search(string: String) {
/* val intent = Intent(this@SearchActivity, SearchResultActivity::class.java) val intent = Intent(this@SearchActivity, SearchResultActivity::class.java)
intent.putExtra("key",string) intent.putExtra("key", string)
startActivity(intent)*/ startActivity(intent)
additem(string) additem(string)
} }
@ -104,14 +112,27 @@ class SearchActivity : BaseActivity<ActivitySearchBinding>() {
override fun onBindViewHolder(holder: VH, position: Int) { override fun onBindViewHolder(holder: VH, position: Int) {
holder.binding.button.text = list[position] holder.binding.button.text = list[position]
holder.binding.button.setOnClickListener { holder.binding.button.setOnClickListener {
val a: Button = it as Button val a: Button = it as Button
search(a.text.toString()) search(a.text.toString())
} }
holder.binding.button.setOnLongClickListener {
val str = list[holder.bindingAdapterPosition]
MaterialAlertDialogBuilder(it.context).setTitle("确定要删除此记录:$str")
.setPositiveButton("确定") { _, _ ->
list.remove(str)
notifyItemRemoved(holder.bindingAdapterPosition)
}.setNegativeButton("取消", null)
.show()
false
}
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {
viewBinding.textview1Text1.isVisible = list.isEmpty() val empty = list.isEmpty()
viewBinding.textview1Text1.isVisible =empty
viewBinding.deleat.isVisible = !empty
return list.size return list.size
} }