diff --git a/app/src/main/java/com/coldmint/rust/pro/SearchActivity.kt b/app/src/main/java/com/coldmint/rust/pro/SearchActivity.kt index fd6a0c6..20814e3 100644 --- a/app/src/main/java/com/coldmint/rust/pro/SearchActivity.kt +++ b/app/src/main/java/com/coldmint/rust/pro/SearchActivity.kt @@ -30,6 +30,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder */ class SearchActivity : BaseActivity() { lateinit var list: MutableList + val sharedPreferencesKey = "search_history" @SuppressLint("CommitPrefEdits", "NotifyDataSetChanged") override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) { @@ -39,32 +40,27 @@ class SearchActivity : BaseActivity() { 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("确定") { _, _ -> - while (list.isNotEmpty()) { - val index = 0 - list.removeAt(index) - adapter.notifyItemRemoved(index) - } - getSharedPreferences("lishi", - 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() + 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(sharedPreferencesKey, + Context.MODE_PRIVATE).edit().putStringSet( + "data", list.toSet()).apply() + } + dialog.setNegativeButton(R.string.dialog_cancel, null) + dialog.show() } viewBinding.searchView.onActionViewExpanded() } @@ -88,7 +84,7 @@ class SearchActivity : BaseActivity() { 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() { } + //TODO:将适配器继承自BaseAdapter,使其不再过度依赖SearchActivity。 val adapter: RecyclerView.Adapter = object : RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH { @@ -128,9 +125,10 @@ class SearchActivity : BaseActivity() { false } } + override fun getItemCount(): Int { val empty = list.isEmpty() - viewBinding.textview1Text1.isVisible =empty + viewBinding.textview1Text1.isVisible = empty viewBinding.deleat.isVisible = !empty return list.size } diff --git a/app/src/main/res/drawable/baseline_delete_24.xml b/app/src/main/res/drawable/baseline_delete_24.xml new file mode 100644 index 0000000..de011dd --- /dev/null +++ b/app/src/main/res/drawable/baseline_delete_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/activity_search.xml b/app/src/main/res/layout/activity_search.xml index 32fd67f..a0f7009 100644 --- a/app/src/main/res/layout/activity_search.xml +++ b/app/src/main/res/layout/activity_search.xml @@ -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" /> @@ -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" /> diff --git a/app/src/main/res/layout/item_string.xml b/app/src/main/res/layout/item_string.xml index 3a4cadd..5260106 100644 --- a/app/src/main/res/layout/item_string.xml +++ b/app/src/main/res/layout/item_string.xml @@ -1,17 +1,18 @@ + android:layout_height="wrap_content"> +