【问题标题】:Android Searchview : Open suggestions list dropdown by defaultAndroid Searchview:默认打开建议列表下拉菜单
【发布时间】:2020-11-27 15:38:12
【问题描述】:

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

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

有没有办法强制这种行为?

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
            }
        })

【问题讨论】:

    标签: android kotlin android-actionbar android-adapter searchview


    【解决方案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
        }
    })
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-18
        相关资源
        最近更新 更多