2011-12-29 24 views
4

我已經將SearchView添加到我的應用程序的ActionBar中,並使用SearchableInfo設置了一個SuggestionsProvider。除非在搜索查詢文本爲空時不顯示任何建議,否則建議實際上運行良好。我發現,即使searchSuggestThreshold設置爲0,我的建議提供者也不會被查詢建議。Android Honeycomb - 如何查詢文本爲空時顯示SearchView的建議?

最接近的解決方案是使用setOnQueryTextFocusChangeListener並在onFocusChange中交換SearchView建議適配器的光標。通過這種方式,我可以看到正確高度的建議,但沒有顯示任何建議文字。

以下是我如何查詢光標以顯示空文本的建議。

Cursor emptySuggestions = getContentResolver().query(
          Uri.parse("content://my.authority/search_suggest_query/test"), 
          null, null, null, null); 

有什麼方法可以使建議正確顯示嗎?

當然,當搜索查詢爲空時,總有可能顯示我自己的ListPopupWindow,但我想避免這種情況,除非有一種簡單的方法來重用SearchView中使用的佈局。

回答

2

我想出如何觸發搜索視圖來查詢我的建議提供了空搜索文本:

_searchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() 
     { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) 
      { 
      if (!hasFocus) 
       return; 

      String query = _searchView.getQuery().toString(); 
      _searchView.setQuery(query, false); 
      } 
     }); 
+0

嗨,你能解釋爲什麼這個工程?爲什麼它不適用於searchView.setQuery(query,false)行? – benzabill 2014-07-03 17:00:55

+0

@benzabill其實我不記得細節,並重新編寫代碼,不再需要它。我想我的SuggestionsProvider中有一個錯誤。 – 2014-07-03 17:18:37

+0

使用此解決方案建議出現在鍵盤後面。我在「view.post(new Runnable(){」)中封裝了代碼,仍然沒有用。如果searchview已經準備好顯示建議,那麼必須有一個處理程序來處理。 – 2014-08-06 09:26:35

1

除了手動觸發onQueryTextChange()方法之上,SearchView.SearchAutoComplete門檻需要設置爲零

SearchView.SearchAutoComplete sac = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); 
if (sac != null) { 
    sac.setThreshold(0); 
} 
+0

即使這種方法受限制,但它的工作原理! – 2018-02-27 13:58:47

相關問題