Android Searchview:默认打开建议列表下拉列表

Android Searchview : Open suggestions list dropdown by default

以下代码允许在我的搜索视图中显示一个小建议 window:

我正在寻找一种方法,当用户单击菜单上的搜索项时默认在开头显示此视图。

有什么办法可以强制这种行为吗?

val from = arrayOf(SearchManager.SUGGEST_COLUMN_TEXT_1)
        val to = intArrayOf(R.id.item_label)
        val cursorAdapter = SimpleCursorAdapter(context, R.layout.search_item, null, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER)
        val suggestions = listOf("Apple", "Blueberry", "Carrot", "Daikon")

        searchView.suggestionsAdapter = cursorAdapter

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                hideKeyboard()
                return false
            }

            override fun onQueryTextChange(query: String?): Boolean {
                val cursor = MatrixCursor(arrayOf(BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1))

                query?.let {
                    suggestions.forEachIndexed { index, suggestion ->
                        if (suggestion.contains(query, true)) {
                            cursor.addRow(arrayOf(index, suggestion))
                        }
                    }
                }

                cursorAdapter.changeCursor(cursor)
                return true
            }
        })

        searchView.setOnSuggestionListener(object: SearchView.OnSuggestionListener {
            override fun onSuggestionSelect(position: Int): Boolean {
                return false
            }

            override fun onSuggestionClick(position: Int): Boolean {
                hideKeyboard()
                val cursor = searchView.suggestionsAdapter.getItem(position) as Cursor
                val selection = cursor.getString(cursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1))
                searchView.setQuery(selection, false)

                // Do something with selection
                return true
            }
        })

您可以尝试做一些事情,例如在 searchView 上添加点击侦听器,如果列表中的字符串包含从 a 到 z 的任何字母,则添加结果以显示整个列表。

searchView.setOnClickListener(View.OnClickListener {
    suggestions.forEachIndexed { index, suggestion ->
        if (suggestion.matches("[a-zA-Z]+")) {
            cursor.addRow(arrayOf(index, suggestion))
        }
    }
})

解决方案

案例 1:SearchView 未展开,用户必须单击搜索按钮才能输入搜索文本。

放在一起。

val searchSrcTextView 
        = searchView.findViewById<AutoCompleteTextView>(R.id.search_src_text)
        
// Set the threshold to 0
searchSrcTextView.threshold = 0

// Listen event when users click on the search view
searchView.setOnSearchClickListener {
    // Fill data to the cursor
    val cursor = MatrixCursor(arrayOf(BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1))
    suggestions.forEachIndexed { index, suggestion ->
        cursor.addRow(arrayOf(index, suggestion))
    }
    cursorAdapter.changeCursor(cursor)

    // Show suggestions drop down view
    searchSrcTextView.showDropDown()
}

情况 2:如果用户在初始状态调用 onActionViewExpanded() 方法,SearchView 将展开。

val searchSrcTextView
        = searchView.findViewById<AutoCompleteTextView>(R.id.search_src_text)

// Set the threshold to 0
searchSrcTextView.threshold = 0

// Fill data to the cursor
val cursor = MatrixCursor(arrayOf(BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1))
suggestions.forEachIndexed { index, suggestion ->
    cursor.addRow(arrayOf(index, suggestion))
}
cursorAdapter.changeCursor(cursor)
searchView.onActionViewExpanded()

// Show suggestions drop down view
searchSrcTextView.post { 
    searchSrcTextView.showDropDown()
}

在这两种情况下,当用户单击 SearchView 时,将显示建议下拉列表。如果用户搜索某些文本,然后通过单击关闭搜索按钮或按键盘上的 CLEAR 键清除搜索文本,建议仍会显示在屏幕上。要避免此行为,您可以使用 setThreshold(int) 设置 SearchView 的阈值。

例子

searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
    override fun onQueryTextSubmit(query: String?): Boolean {
        hideKeyboard()
        return false
    }

    override fun onQueryTextChange(query: String?): Boolean {
        // Add this code to set threshold to expected value
        if (query?.isEmpty() == true) {
            searchSrcTextView.threshold = 1
        }

        val cursor = MatrixCursor(arrayOf(BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1))

        query?.let {
            suggestions.forEachIndexed { index, suggestion ->
                if (suggestion.contains(query, true)) {
                    cursor.addRow(arrayOf(index, suggestion))
                }
            }
        }

        cursorAdapter.changeCursor(cursor)
        return true
    }
})