android源码解析 ---- QuickSearchBox 搜索

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

该模块说明:

  每一次输入框的内容发生改变时,都会进行搜索查询,并显示搜索建议。本模块将字符串分别发送给搜索设置中的各个应用程序,由应用程序的provider在各自的模块中进行查询,将查询结果返回给搜索模块,然后搜索模块将搜索建议显示出来。

  如果点击查询按钮进行搜索,那么此时就会跳转到其他的应用程序中,如短信息。那么就由短信息模块进行查找并将结果显示在信息模块的某个界面上。

  即,无论搜索建议还是点击搜索按钮,其搜索的查询功能均在各个模块完成,由各个模块在其自己定义的数据库中进行查找,并提供结果,不同之处在于:搜索建议的结果要显示在搜索模块列表,搜索的结果要显示在提供搜索的应用程序模块。

 

1.启动界面Activity

   点击硬件搜索键或应用程序,启动的第一个界面对应的类为SearchActivity.java

   对应的布局文件          R.layout.search_activity

   左侧应用程序选择按钮 mCorpusIndicator

   搜索内容输入框          mQueryTextView

   开始搜索按钮             mSearchGoButton

   搜索建议内容显示显示列表  mSuggestionsView  

   搜索建议对应的填充内容     mSuggestionsAdapter

2.应用程序选择对话框

   点击mCorpusIndicator -> showCorpusSelectionDialog() ->createCorpusSelectionDialog()创建对话框

   CorpusSelectionDialog.java类即为所显示的对话框类

   对应布局文件                R.layout.corpus_selection_dialog   

   对话框右上角设置按钮    mEditItems

   填充的选项gridView      mCorpusGrid

   内容对应的adapter       onStart() -> CorporaAdapter adapter=CorporaAdapter.createGridAdapter(getViewFactory(),...)

   CorporaAdapter.java:对话框填充内容adapter

        new CorporaAdapter(...)  -> getView(...)生成整个布局内容

        createView(...) -> mViewFactory.createGridCorpusView(parent)调用

   CorpusSelectionDialog.java中getViewFactory()->QsbApplication.java中getCorpusViewFactory()->createCorpusViewFactory

           ->new CorpusViewInflater(getContext())

   CorpusViewInflater.java中createGridCorpusView(...) 对应布局R.layout.corpus_grid_item

   说明: CorporaAdapter.java中的mViewFactory对应的方法,在CorpusViewInflater.java中,其实现了CorpusViewFactory接口

   

  3. 搜索建议内容

     说明:当编辑textView时,即textView内容每次发生改变的时候,都会调用一次搜索功能,并将搜索建议所得的内容显示在下面的listView中。其中,各模块的搜索功能由各个模块个字完成,而搜索模块只是将各模块的搜索结果显示出来。

     代码跟踪:searchActivity.java中

     onCreate()-->mQueryTextView.addTextChangedListener(new SearchTextWatcher());

     SearchTextWatcher-->updateSuggestionsBuffered();

     updateSuggestionsBuffered-->mHandler.postDelayed(mUpdateSuggestionsTask, delay);
     mUpdateSuggestionsTask-->updateSuggestions(getQuery());
     updateSuggestions-->getSuggestionsProvider().getSuggestions(query, mCorpus, getMaxSuggestions());
     mSuggestionsAdapter.setSuggestions(suggestions);

     此处跟踪寻找suggestions,即查找getSuggestionsProvider().getSuggestions(query, mCorpus, getMaxSuggestions())函数在哪里调用的。

     searchActivity.java: getSuggestionsProvider-->getQsbApplication().getSuggestionsProvider();
     QsbApplication.java: getSuggestionsProvider-->mSuggestionsProvider = createSuggestionsProvider();
                                    provider = new SuggestionsProviderImpl(...)

     此provider即位获取suggestions的provider。现在进入SuggestionsProviderImpl.java中,跟踪其getSuggestions(...)函数。该函数中的QueryTask.startQueries(...)即位调用查找的关键语句。

 

如上分析意思:

      每次搜索输入框内容发生改变时,会调用updateSuggestions(String query) {。。。}函数,更新列表内容。而对应搜索内容的查询,在SuggestionsProviderImpl.java中的getSuggestions(...)函数中。

      SuggestionsAdapter.java为搜索建议的adapter

      DefaultSuggestionView.java响应搜索建议的点击事件

  4.搜索设置

     在弹出的选择对话框中,点击右上角的图片,进入搜索设置界面。SearchableItemsSettings.java。

     同样,在设置模块中也有这样一个入口。SearchSettings.java,corporaPreference响应进入搜索设置界面。

     其资源文件:R.xml.preferences_searchable_items

     此文件中动态加载响应搜索功能的模块的CheckBoxPreference,当preference属性发生改变时,会发送广播

            SearchSettings.broadcastSettingsChanged(this);

     在CorporaUpdateReceiver.java中,更新搜索设置和小部件搜索列表

 

     检索哪一个状态为开启或关闭状态:

     在SearchableItemsSettings.java中,函数createCorpusPreference(...)中

      sourcePref.setDefaultValue(corpus.isCorpusDefaultEnabled());决定了该CheckBoxPreference处于打开还是关闭状态

     响应函数在AbstractCorpus.java中, isCorpusDefaultEnabled(){...}

     Config.java:isCorpusEnabledByDefault(...)

     系统出厂默认状态为网络/联系人/应用程序,为开启状态,在R.array.default_corpora中声明,

       若想修改出厂默认选中项,修改default_corpora内容即可

     通过比较mDefaultCorpora.contains(corpusName)来返回boolean,即是否为开启状态

  5.xml文件

     choice_activity.xml   搜索小部件的配置活动布局

     corpus_indicator.xml  搜索框左侧的选择按钮,默认为全部

     corpus_list_item.xml     小部件配置活动中的ListView对应的布局文件

     search_activity.xml      应用程序主界面

     corpus_selection_dialog.xml   搜索栏左侧点击,弹出的应用程序选择对话框的布局

     corpus_grid_item.xml   如上dialog,应用程序选择对话框的内容的布局

转载于:https://my.oschina.net/u/782213/blog/160063

你可能感兴趣的:(android源码解析 ---- QuickSearchBox 搜索)