片段中最近的查询建议

时间:2015-10-06 15:24:47

标签: android android-fragments searchview

我在SearchView中将Fragment实施为this topic中建议的 @David 。有谁知道如何在Fragment中向SearchView添加最近查询建议

2 个答案:

答案 0 :(得分:2)

来自android documentation

步骤1-创建内容提供商

Creathe MySuggestionProvider类,我的位于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类所在的路径。我的位于searchviewpager / utils / MySuggestionProvider->中,因此名称为.utils.MySuggestionProvider

android:authorities:。它与您在RecommendationProvider中放入的字符串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类中输入的字符串授权相同。

android:searchSuggestSelection:这只是一个空格和一个问号。

第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类中

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只是重新启动活动并将查询发送给它。然后,要处理建议中的点击,您应该添加以下代码:

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

步骤1-将launchMode设置为singleTop

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

<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是必要的,因为您将活动设置为singleTop,当应用程序收到搜索时,它不会重新创建活动并调用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才能正常工作。然后,转到此文件@ layout / abc_search_dropdown_item_icons_2line并复制相同的ID。

如果很难找到此abc文件,则可以将@ layout / my_search_dropdown_item_icons替换为-> @ layout / abc_search_dropdown_item_icons_2line->然后将光标置于“ abc_search ...”并按Ctrl + B。您将重定向到该文件,您可以在其中使用相同的ID。

答案 1 :(得分:0)

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

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

这是一个示例实现,说明如何将片段中的搜索视图与科特林-

中的最近建议查询进行集成
  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字段具有与RecommendationionProvider.kt AUTHORITY字段相同的路径)

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

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