Fragment 中的最近查询建议

Recent Query Suggestions in Fragment

我在 Fragment 中实现了 SearchView 作为 this topic 中建议的 @David。有谁知道如何将 Recent Query Suggestions 添加到 Fragment 中的 SearchView?

来自 android documentation.

第 1 步 - 创建内容提供程序

创建 MySuggestionProvider class,我的位于 searchviewpager/utils/MySuggestionProvider

package com.soon.karat.searchviewpager.utils;

import android.content.SearchRecentSuggestionsProvider;

public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
    // AUTHORITY is a unique name, but it is recommended to use the name of the 
    // package followed by the name of the class.
    public final static String AUTHORITY = "com.soon.karat.searchviewpager.utils.MySuggestionProvider";

    // Uncomment line below, if you want to provide two lines in each suggestion:
    // public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;
    public final static int MODE = DATABASE_MODE_QUERIES;

    public MySuggestionProvider() {
        setupSuggestions(AUTHORITY, MODE);
    }
}

第 2 步 - 将提供商添加到清单中

在应用程序级别的清单中添加以下代码行

<application...>
        <provider
            android:name=".utils.MySuggestionProvider"
            android:authorities="com.soon.karat.searchviewpager.utils.MySuggestionProvider" />

android:name: 就是你的SuggestionProvider class所在的路径。我的位于 searchviewpager/utils/MySuggestionProvider --> 因此名称将是 .utils.MySuggestionProvider

android:authorities: 与您在 SuggestionProvider 中输入的字符串 AUTHORITY 相同,应该是相同的名称。

第 3 步 - 将搜索添加到 searchable.xml

将最后两行(searchSuggestAuthority 和 searchSuggestSelection)添加到您的 searchable.xml

<?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/search_for_anything"
    android:searchSuggestAuthority="com.soon.karat.searchviewpager.utils.MySuggestionProvider"
    android:searchSuggestSelection=" ?"/>

android:searchSuggestAuthority: 这与您在 MySuggestionProvider class.

中输入的字符串 AUTHORITY 相同

android:searchSuggestSelection: 只是一个space和一个问号。

第 4 步 - 保存查询

每当用户提交查询时,您都应将其保存在 SuggestionProvider 中,假设您将其保存在 onQueryTextSubmit

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Log.i(TAG, "onQueryTextSubmit: Query was submitted");

            // -------------------------------------------------------
            //           Lines responsible to save the query 
            // -------------------------------------------------------
            SearchRecentSuggestions suggestions = new SearchRecentSuggestions(MainActivity.this,
                    MySuggestionProvider.AUTHORITY,
                    MySuggestionProvider.MODE);
            suggestions.saveRecentQuery(query, null);
            // -------------------------------------------------------

            displaySearchResults(query);

            // The listener can override the standard behavior by returning true to indicate that it has
            // handled the submit request. Otherwise return false to let the SearchView handle the
            // submission by launching any associated intent.
            return true;
        }

附加值

隐藏键盘并关闭 SearchView

也许您想在用户完成向您的应用提交查询时隐藏键盘并关闭搜索视图。为此,您使用以下代码行。

private void dismissKeyBoardAndSearchView() {
    Helpers.hideKeyBoard(this);
    // You should check if MenuItem is not null, otherwise the app would crash when rotating the screen 
    if (searchMenuItem != null) {
        searchMenuItem.collapseActionView();
    }
}

searchMenuItem 是一个 MenuItem 并且它与您在 onCreateOptionsMenu 中创建 SearchView 时使用的相同,您只需在全局声明它以在其他方法中访问或将其解析为方法:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    // -----------------------------------------------
    //     This is the MenuItem you will collapse
    // -----------------------------------------------
    searchMenuItem = menu.findItem(R.id.menu_search);
    // -----------------------------------------------
    SearchView searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setQueryRefinementEnabled(true);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

隐藏键盘在 Helpers 中 Class

public class Helpers {

public static void hideKeyBoard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    View view = activity.getCurrentFocus();
    if (view == null) {
        view = new View(activity);
    }
    assert inputMethodManager != null;
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

}

当用户点击建议时处理

您可能会注意到,当用户单击建议时,不会发生任何事情,因为没有调用 onQueryTextSubmit。 Android 只需重新启动 activity 并将查询发送给它。然后,要处理建议中的点击,您应该添加以下代码:

假设进行搜索的 activity 与接收搜索的相同:

第 1 步 - 将启动模式设置为 singleTop

在清单中将您的 activity launchMode 设置为 singleTop 以防止系统在用户进行多次搜索时多次重新创建 activity。

<application...>
    <provider
        android:name=".utils.MySuggestionProvider"
        android:authorities="com.soon.karat.searchviewpager.utils.MySuggestionProvider" />
    <activity android:name=".SearchableActivity"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

第 2 步 - 在 onCreate 和 onNewIntent

中处理意图

您应该在 onCreate 和 onNewItent 中处理搜索意图。 onNewIntent 是必需的,因为您将 activity 设置为 singleTop,当应用收到搜索时,它不会重新创建 activity 并调用 onCreate,它只会调用 onNewIntent.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    handleIntent(getIntent());
}


@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

自定义您最近的查询建议布局

也许,您不喜欢最近查询建议的显示方式,那么您可以通过简单的方式更改其布局。

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <!-- Add this line -->
    <item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>

<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView" >
    <item name="suggestionRowLayout">@layout/my_search_dropdown_item_icons</item>
</style>

my_search_drodown_item_icons: 这是您创建并自定义的布局。

警告:您应该使用与之前布局相同的 ID android 以使其正常工作。然后,转到此文件@layout/abc_search_dropdown_item_icons_2line 并复制相同的 ids.

如果你很难找到这个abc文件,你可以把@layout/my_search_dropdown_item_icons换成-->@layout/abc_search_dropdown_item_icons_2line-->然后把光标放到"abc_search..."然后按Ctrl +B。您将被重定向到该文件,您可以在其中使用相同的 ID。

好的,所以我在 github 上的这个特定存储库的帮助下找到了这个解决方案,感谢并感谢他。

https://github.com/nex3z/android-examples/tree/master/CustomSearchSuggestionItem

这是一个示例实现,说明如何将片段中的搜索视图与 Kotlin 中的最近建议查询集成 -

  1. 创建显示最近列表的适配器

    SearchAdapter.java

     public class SearchAdapter extends CursorAdapter {
    
     private static final String LOG_TAG = 
     SearchAdapter.class.getSimpleName();
    
     public SearchAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
      }
    
     @Override
     public View newView(Context context, Cursor cursor, ViewGroup parent) {
     View view = LayoutInflater.from(context).inflate(
               R.layout.search_item, parent, false);
    
     ViewHolder viewHolder = new ViewHolder(view);
     view.setTag(viewHolder);
    
     return view;
      }
    
     @Override
     public void bindView(View view, Context context, Cursor cursor) {
       ViewHolder viewHolder = (ViewHolder) view.getTag();
       viewHolder.mTitle.setText(
       cursor.getString(cursor.
           getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1)));
         }
    
      public String getSuggestionText(int position) {
        if (position >= 0 && position < getCursor().getCount()) {
        Cursor cursor = getCursor();
        cursor.moveToPosition(position);
        return cursor.getString(cursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
        }
        return null;
          }
    
        public static class ViewHolder {
         public TextView mTitle;
    
            public ViewHolder(View view) { 
            // view of your custom layout
            mTitle = (TextView) view.findViewById(R.id.text);
              }
           }
         }
    

    search_item.xml(内部布局文件夹)

      <TextView xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tool="http://schemas.android.com/tools"
      android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="50dp"
      android:fontFamily="@font/poppins_bold"
      android:textColor="@color/colorPrimary"
      android:gravity="start|center"
      android:paddingStart="30dp"
      android:paddingEnd="30dp"
      tool:text="hello" />
    

    menu.xml(将其放入您的 menu.xml 文件)

    <item
    android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="@string/search"
    app:actionViewClass="androidx.appcompat.widget.SearchView"
    app:showAsAction="always" />
    

    SuggestionProvider.kt

     class SuggestionProvider : SearchRecentSuggestionsProvider() {
       init {
        setupSuggestions(AUTHORITY, MODE)
       }
    
       companion object {
        // Set As Path To This File
        const val AUTHORITY = "com.your_package_name.SuggestionProvider"
        const val MODE: Int = DATABASE_MODE_QUERIES
       }
     }
    

    AndroidManifest.xml(确保 android:authrotities 字段具有与 SuggestionProvider.kt 权限字段相同的路径)

      <application>
       .....
        <provider
        android:name=".utils.SuggestionProvider"
        android:authorities="com.you_package_name.SuggestionProvider" />
    
       .....
       </application>
    

    YourFragment.kt(将此代码放入您已创建的片段中,您希望在操作栏中包含搜索栏)

    private var mSuggestionAdapter: SearchAdapter? = null
    private lateinit var searchView: SearchView
    private var queryTextListener: SearchView.OnQueryTextListener? = null
    
    
    
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.action_search ->
            return false
    }
    searchView.setOnQueryTextListener(queryTextListener)
    return super.onOptionsItemSelected(item)
    }
    
    
    
    
    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    inflater.inflate(R.menu.menu, menu)
    val searchItem = menu.findItem(R.id.action_search)
    val searchManager =
        activity!!.getSystemService(Context.SEARCH_SERVICE) as SearchManager
    
    mSuggestionAdapter = SearchAdapter(activity, null, 0)
    
    if (searchItem != null) {
        searchView = searchItem.actionView as SearchView
    }
    if (searchView != null) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(activity!!.componentName))
        searchView.suggestionsAdapter = mSuggestionAdapter;
    
        queryTextListener = object : SearchView.OnQueryTextListener {
            override fun onQueryTextChange(newText: String): Boolean {
                // Update Cursor With Each Query Text Change
                val cursor = getRecentSuggestions(newText)
                if (cursor != null) {
                    mSuggestionAdapter?.swapCursor(cursor)
                }
                return false
            }
    
            override fun onQueryTextSubmit(query: String): Boolean {
    
                // Save Submitted Query To Adapter
                val suggestions = SearchRecentSuggestions(
                    activity,
                    SuggestionProvider.AUTHORITY, SuggestionProvider.MODE
                )
                suggestions.saveRecentQuery(query, null)
    
                // Do Your Search Stuff Here With Query
                return true
            }
        }
    
        searchView.setOnSuggestionListener(object : SearchView.OnSuggestionListener {
            override fun onSuggestionSelect(position: Int): Boolean {
                return false
            }
    
            override fun onSuggestionClick(position: Int): Boolean {
            // On Clicking Suggestion Load It To Submit Query Listener    
    
          searchView.setQuery(mSuggestionAdapter?.getSuggestionText(position), true)
                return true
            }
        })
    
    
        searchView.setOnQueryTextListener(queryTextListener)
    
         }
         super.onCreateOptionsMenu(menu, inflater)
        }
    
    
        // Function To Retrieve Suggestion From Content Resolver
        fun getRecentSuggestions(query: String): Cursor? {
         val uriBuilder = Uri.Builder()
        .scheme(ContentResolver.SCHEME_CONTENT)
        .authority(SuggestionProvider.AUTHORITY)
    
        uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY)
    
         val selection = " ?"
         val selArgs = arrayOf(query)
    
         val uri = uriBuilder.build()
        return activity?.contentResolver?.query(uri, null, selection, selArgs, null)
         }
    

就是这样,您的片段中有一个搜索栏,您可以控制您处理搜索和最近建议的方式的查询结果。