调整历史记录的对话框样式。

This commit is contained in:
Cold-Mint 2024-03-02 15:41:28 +08:00
parent ef95908cd4
commit 0f7c318850
5 changed files with 38 additions and 32 deletions

View File

@ -30,6 +30,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
*/
class SearchActivity : BaseActivity<ActivitySearchBinding>() {
lateinit var list: MutableList<String>
val sharedPreferencesKey = "search_history"
@SuppressLint("CommitPrefEdits", "NotifyDataSetChanged")
override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) {
@ -39,32 +40,27 @@ class SearchActivity : BaseActivity<ActivitySearchBinding>() {
viewBinding.hotSearchView.layoutManager = StableLinearLayoutManager(this)
loadSearchView()
loadHotSearch()
list = getSharedPreferences("lishi", Context.MODE_PRIVATE)
list = getSharedPreferences(sharedPreferencesKey, Context.MODE_PRIVATE)
.getStringSet("data", mutableSetOf())
?.toMutableList() ?: mutableListOf()
viewBinding.hotSearchView2.layoutManager = FlexboxLayoutManager(this)
viewBinding.hotSearchView2.adapter = adapter
viewBinding.deleat.setOnClickListener {
MaterialAlertDialogBuilder(this@SearchActivity)
.setTitle("清空所有历史记录")
.setPositiveButton("确定") { _, _ ->
val dialog = MaterialAlertDialogBuilder(this@SearchActivity);
dialog.setTitle(R.string.search_history)
dialog.setMessage(R.string.clear_all_history)
dialog.setPositiveButton(R.string.dialog_ok) { _, _ ->
while (list.isNotEmpty()) {
val index = 0
list.removeAt(index)
adapter.notifyItemRemoved(index)
}
getSharedPreferences("lishi",
getSharedPreferences(sharedPreferencesKey,
Context.MODE_PRIVATE).edit().putStringSet(
"data", list.toSet()).apply()
/* CoroutineScope(Dispatchers.Main).launch {
while (list.isNotEmpty()) {
list.removeAt(0) // 删除第一个数据
adapter.notifyItemRemoved(0) // 刷新 RecyclerView
delay(300) // 每隔一秒执行一次删除操作
}
}*/
}
.setNegativeButton("取消", null).show()
dialog.setNegativeButton(R.string.dialog_cancel, null)
dialog.show()
}
viewBinding.searchView.onActionViewExpanded()
}
@ -88,7 +84,7 @@ class SearchActivity : BaseActivity<ActivitySearchBinding>() {
if (list.size > 10) {
list.removeAt(list.lastIndex)
}
val editor = getSharedPreferences("lishi", Context.MODE_PRIVATE).edit()
val editor = getSharedPreferences(sharedPreferencesKey, Context.MODE_PRIVATE).edit()
editor.putStringSet("data", list.toSet())
editor.apply()
}
@ -101,6 +97,7 @@ class SearchActivity : BaseActivity<ActivitySearchBinding>() {
}
//TODO:将适配器继承自BaseAdapter使其不再过度依赖SearchActivity。
val adapter: RecyclerView.Adapter<VH> = object : RecyclerView.Adapter<VH>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
@ -128,6 +125,7 @@ class SearchActivity : BaseActivity<ActivitySearchBinding>() {
false
}
}
override fun getItemCount(): Int {
val empty = list.isEmpty()
viewBinding.textview1Text1.isVisible = empty

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
</vector>

View File

@ -60,7 +60,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="历史记录"
android:text="@string/search_history"
card_view:ignore="HardcodedText" />
<ImageView
@ -68,7 +68,7 @@
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginEnd="6dp"
android:src="@drawable/auto_delete"
android:src="@drawable/baseline_delete_24"
card_view:ignore="ContentDescription" />
</LinearLayout>
@ -109,8 +109,8 @@
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="6dp"
android:background="?android:colorBackground"
android:paddingHorizontal="6dp"
android:paddingTop="6dp"
android:visibility="gone"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />

View File

@ -1,17 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
android:layout_height="wrap_content">
<Button
android:id="@+id/button"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:strokeWidth="1dp"
app:strokeColor="?attr/colorOnSurface"
app:shapeAppearance="?attr/shapeAppearanceSmallComponent"
android:text="@string/action"
android:textColor="?attr/colorOnSurface"
app:rippleColor="?colorBackgroundFloating"
android:text="@string/action" />
app:shapeAppearance="?attr/shapeAppearanceSmallComponent"
app:strokeColor="?attr/colorOnSurface"
app:strokeWidth="1dp" />
</RelativeLayout>

View File

@ -971,4 +971,6 @@
<string name="code_demo">示例代码</string>
<string name="clipboard_cue">剪切板提示</string>
<string name="clipboard_cue_tip">在安卓13及以上系统应用将内容复制到剪贴板时弹出提示。</string>
<string name="clear_all_history">清空所有历史记录</string>
<string name="search_history">搜索历史</string>
</resources>