SearchView 不显示最近的查询建议

SearchView doesn't show recent query suggestions

提问人:VortexDream 提问时间:10/29/2023 更新时间:10/29/2023 访问量:21

问:

我正在学习 Android 开发,并尝试按照官方文档添加最近的查询建议:链接。当我启动应用程序时,根本没有任何建议。搜索本身工作正常。我的 searchView 位于工具栏中,整个应用程序由单个活动和片段组成。我设法使用 RecyclerView 和 SharedPreferences 来做到这一点,但它看起来不那么优雅和漂亮,需要重构我的旧代码。我看过相关的问题,但没有什么能真正回答我的。

清单 .xml:

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name=".PhotoGalleryApplication"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.PhotoGallery"
        tools:targetApi="31">
        <provider
            android:name="com.vortex.android.photogallery.SuggestionProvider"
            android:authorities="com.vortex.android.photogallery.SuggestionProvider"
            android:exported="false"
            android:enabled="true" />
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.SEARCH" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

可搜索的 .xml:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="TEST"
    android:searchSuggestAuthority="com.vortex.android.photogallery.SuggestionProvider"
    android:searchSuggestSelection=" ?">
</searchable>

SuggestionProvider.kt:

class SuggestionProvider: SearchRecentSuggestionsProvider() {

    init {
        setupSuggestions(AUTHORITY, MODE)
    }

    companion object {
        const val AUTHORITY = "com.vortex.android.photogallery.SuggestionProvider"
        const val MODE: Int = DATABASE_MODE_QUERIES
    }
}

我的工具栏布局:

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/menu_item_search"
        android:title="@string/search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="ifRoom" />

    <item android:id="@+id/menu_item_clear"
        android:title="@string/clear_search"
        app:showAsAction="never" />
</menu>

我在 Fragment 类中的部分代码:

override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
        menuInflater.inflate (R.menu.fragment_photo_gallery, menu)

        val searchItem: MenuItem = menu.findItem(R.id.menu_item_search)
        searchView = searchItem.actionView as? SearchView
        val searchManager = requireContext().getSystemService(Context.SEARCH_SERVICE) as SearchManager

        searchView?.setSearchableInfo(searchManager.getSearchableInfo(activity?.componentName))

        searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

            override fun onQueryTextSubmit(query: String?): Boolean {
                ...
                handleSearchQuery(query)
      
                if (query != photoGalleryViewModel.galleryItem.value.query) {
                    binding.progressBar.visibility = View.VISIBLE
                    binding.photoGrid.visibility = View.INVISIBLE

                    photoGalleryViewModel.setQuery(query ?: "")
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return false
            }
        })
    }

private fun handleSearchQuery(query: String?) {
        query?.let {
            SearchRecentSuggestions(
                requireContext(),
                SuggestionProvider.AUTHORITY,
                SuggestionProvider.MODE
            ).saveRecentQuery(it, null)
        }
    }
Kotlin 碎片 Android工具栏

评论


答: 暂无答案