简体   繁体   English

Android搜索界面-显示多个SearchViews

[英]Android Search Interface — Multiple SearchViews being displayed

I'm trying to incorporate search functionality into one of my applications and I'm having problems solving an issue with multiple SearchViews being displayed. 我正在尝试将搜索功能合并到我的一个应用程序中,但是在解决显示多个SearchView的问题时遇到了问题。
So, when I click the “search” icon in my actionbar I get a “search view” that occupies the entire actionbar and there is also another “search view” that is either hiding below the top view or it is rendered after the top view is destroyed, I'm not sure which as I haven't been able to track it down. 因此,当我单击操作栏中的“搜索”图标时,我会得到一个占据整个操作栏的“搜索视图”,还有另一个“搜索视图”隐藏在顶视图下方或在顶视图之后呈现被销毁了,我不确定是哪一个,因为我一直无法找到它。 The SearchView that is rendered on the bottom/second is the actual view that I want. 在底部/秒呈现的SearchView是我想要的实际视图。 The top view functions correctly when I make a search request, but when I navigate back to the activity that made the search call, the original SearchView is gone and the correct SearchView is now in place. 当我发出搜索请求时,顶视图可以正常运行,但是当我导航回到进行搜索调用的活动时,原始SearchView消失了,并且正确的SearchView现在就位了。 It then functions normally until I close/collapse the SearchView. 然后它可以正常工作,直到我关闭/折叠SearchView。 The original SearchView can also be closed by a touchEvent anywhere on the screen and the correct SearchView is up and waiting for input. 原始SearchView也可以通过touchEvent在屏幕上的任意位置关闭,并且正确的SearchView已启动并等待输入。

Any ideas on what is causing this? 关于什么原因的任何想法?

Bar before touching search icon: 触摸搜索图标前的栏:

Bar with both Views: 带有两个视图的栏:

Bar with Correct View: 具有正确视图的栏:

Here is my searchable.xml: 这是我的searchable.xml:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/search_label"
    android:hint="@string/search_hint"
    android:searchSettingsDescription="@string/settings_description" />

My menu xml file: 我的菜单xml文件:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
      android:title="@string/menu_search"
      android:icon="@drawable/ic_menu_search"
      android:orderInCategory="0"
      android:actionViewClass="android.widget.SearchView"
      android:showAsAction="ifRoom|collapseActionView"  />

A snippet of my manifest: 我的清单的摘要:

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
        android:windowSoftInputMode="adjustPan" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

     <activity android:name="SearchMainActivity"
              android:finishOnTaskLaunch ="true"
              android:configChanges="keyboard|keyboardHidden|orientation"
              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>


    <meta-data
            android:name="android.app.default_searchable"
            android:value="SearchMainActivity" />

Main Activity where onCreateOptionsMenu is called: 调用onCreateOptionsMenu的主要活动:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.options_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.search);
    if(searchItem != null && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)) {
        SearchView searchView = (SearchView) searchItem.getActionView();
        if(searchView != null) {
            SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);
        }
    }
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search:
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                startSearch(null, false, Bundle.EMPTY, false);
                return true;
            }
            break;
        default:
            return false;
    }
    return super.onOptionsItemSelected(item);
}

My SearchActivity.java 我的SearchActivity.java

public class SearchMainActivity extends FragmentActivity implements SearchListFragment.OnSearchListItemSelectedListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        SearchQuery.query = intent.getStringExtra(SearchManager.QUERY);
        SearchQuery.original_query = SearchQuery.query;
        SearchQuery.query = SearchQuery.query.toLowerCase().trim().replaceAll("'", "''").replaceAll("[^A-Za-z0-9\\.\\-\\'\\\" ]", "");
    }
    setContentView(R.layout.search_main_layout);

    if(findViewById(R.id.fragment_container) != null) {
        if(savedInstanceState != null) {
            return;
        }
        SearchListFragment listFrag = new SearchListFragment();
        listFrag.setArguments(getIntent().getExtras());

        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, listFrag).commit();
    }
}

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    SearchQuery.query = intent.getStringExtra(SearchManager.QUERY);
    startActivity(intent);
}

@Override
public void onSearchListItemSelected(int position, View v) {
    SearchDetailFragment detailFrag = (SearchDetailFragment)
            getSupportFragmentManager().findFragmentById(R.id.main_list_detail_fragment);
    if(detailFrag != null) {
        detailFrag.updateMainDetailView(position);
    }
    else {
        SearchDetailFragment searchDetailFragment = new SearchDetailFragment();

        Bundle args = new Bundle();
        args.putInt(BaseDetailFragment.CURRENT_LIST_POSITION, position);
        searchDetailFragment.setArguments(args);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.fragment_container, searchDetailFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}
}

It seems like you're double-implementing the search interface in 2 different ways. 您似乎正在以两种不同的方式双重实现搜索界面。 If you take a look at the official docs on setting up a search interface in the action bar , you'll note it makes no mention of calling startSearch in onOptionsItemSelected. 如果您在操作栏中查看有关设置搜索界面官方文档 ,您会注意到它没有提到在onOptionsItemSelected中调用startSearch。 Try removing that from onOptionsItemSelected and see what happens. 尝试从onOptionsItemSelected中删除它,然后看看会发生什么。

Edit: Upon further research, it looks like you might have tried implementing both of the implementations outlined here , but only one or the other is necessary (though one only works on Honeycomb and above unless you're using ABS or AppCompat). 编辑:经过进一步的研究,您似乎可能已经尝试实现此处概述的两种实现,但是只有一种或另一种是必要的(尽管除非您使用的是ABS或AppCompat,否则一种仅适用于Honeycomb及更高版本)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM