SearchView提示不显示单个字符类型

luc*_*mon 21 android autocomplete searchview

我正在开发一个用Scala编写的Android应用程序,该应用程序在android.support.v7.widget.SearchView被覆盖的操作栏内部使用android.support.v7.widget.Toolbar.

在应用程序中,我需要为此启用搜索建议SearchView.因此,每次检测到查询更改时,都会检查是否需要更新建议(下面的代码).如果需要更新建议,则会对后端进行调用,并android.database.MatrixCursor创建一个包含搜索建议的建议,并在SearchViews建议适配器上设置(下面的代码).

我遇到的问题是,在键入单个字符时,搜索提示不会显示.在搜索框中键入两个或多个字符可以很好地工作.

我尝试使用android:searchSuggestThresholdset to 01我的XML配置,我得到相同的结果(比如忽略该选项):搜索提示显示多个输入字符,但不显示单个字符.

我在配置/初始化中遗漏了什么SearchView?像android:searchSuggestThreshold我可以设置的选项一样?我花了最后几个小时寻找其他选择但却找不到.

我看到的一个可能的解决方案是获得AutoCompleteTextView支持SearchView输入并将其阈值设置为1,但这是一个丑陋的(hack-ish)解决方案,因为AutoCompleteTextView它没有被SearchViewAPI 暴露.

有谁知道这个优雅的解决方案?

谢谢!

检测搜索字词更改:

//this gets called whenever something is changed in the search box. "s" contains the entire search term typed in the search box
def onQueryTextChange(s: String): Boolean = {
  //this checks to see if a call should be made to the backend to update suggestions
  SearchSuggestionController.query(s) 
  //return false as we're not consuming the event
  false
}
Run Code Online (Sandbox Code Playgroud)

更新搜索建议:

def updateSuggestions(suggestions: Array[String]): Unit = {
  val cursor = new MatrixCursor(Array(BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1), suggestions.length)
  for{
    i <- suggestions.indices
    s = suggestions(i)
  } cursor.addRow(Array[AnyRef](i.toString, s))

  getActivity.runOnUiThread(() => {
    searchSuggestionAdapter.changeCursor(cursor)
    searchSuggestionAdapter.notifyDataSetChanged()
  })
}
Run Code Online (Sandbox Code Playgroud)

菜单SearchView初始化:

override def onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) = {
    inflater.inflate(R.menu.menu_products, menu)
    val searchManager = getActivity.getSystemService(Context.SEARCH_SERVICE).asInstanceOf[SearchManager]
    val actionItem = menu.findItem(R.id.action_search)
    searchView = MenuItemCompat.getActionView(actionItem).asInstanceOf[SearchView]
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity.getComponentName))
    searchView.setSubmitButtonEnabled(false)
    SearchSuggestionController.onSuggestionsProvided_=(updateSuggestions)
    searchView setSuggestionsAdapter searchSuggestionAdapter
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
      //...
      //change listener for detecting search changes presented above
      //...
    }
  //...
  //initialise other components 
  //...
}
Run Code Online (Sandbox Code Playgroud)

可搜索的配置:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/actionbar_products_search_hint"
    android:searchSuggestThreshold="0"
    android:searchSuggestSelection=" ?"/>
Run Code Online (Sandbox Code Playgroud)

kli*_*mat 0

我在这里看到你关于“优雅的解决方案”的观点,这非常好,但我担心如果你想实现你的目标,Android API 会多次迫使你做“不优雅的”:)

我不知道直接在AutoCompleteTextView作品上设置阈值的解决方法,但如果没有,我建议您再做一个(不是很好)步骤,这可能会起作用:

获取 EditText 的实例:

AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
Run Code Online (Sandbox Code Playgroud)

并强制在 onQueryTextChange 中显示文本长度等于 1 的建议:

if(s.length() == 1){
   searchText.showDropDown();
{
Run Code Online (Sandbox Code Playgroud)