Android 搜索界面——显示多个搜索视图 [英] Android Search Interface -- Multiple SearchViews being displayed

查看:57
本文介绍了Android 搜索界面——显示多个搜索视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将搜索功能合并到我的一个应用程序中,但我在解决显示多个 SearchView 的问题时遇到了问题.
因此,当我单击操作栏中的搜索"图标时,我会得到一个占据整个操作栏的搜索视图",并且还有另一个搜索视图"隐藏在顶视图下方或在顶视图之后呈现被摧毁了,我不确定是哪个,因为我无法追踪它.在底部/秒呈现的 SearchView 是我想要的实际视图.当我发出搜索请求时,顶视图功能正常,但是当我导航回进行搜索调用的活动时,原始 SearchView 消失了,现在正确的 SearchView 就位.然后它会正常运行,直到我关闭/折叠 SearchView.原始 SearchView 也可以通过屏幕上任意位置的 touchEvent 关闭,并且正确的 SearchView 已启动并等待输入.

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.
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. 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. It then functions normally until I close/collapse the 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.

对导致这种情况的原因有什么想法吗?

Any ideas on what is causing this?

触摸搜索图标前的栏:

具有两种视图的栏:

具有正确视图的栏:

这是我的 searchable.xml:

Here is my 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" />

我的菜单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"  />

我的清单片段:

<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" />

调用 onCreateOptionsMenu 的主要活动:

Main Activity where onCreateOptionsMenu is called:

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

我的 SearchActivity.java

My 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();
    }
}
}

推荐答案

您似乎在以两种不同的方式双重实现搜索界面.如果您查看有关在操作栏中设置搜索界面的官方文档,您会注意到它没有提到在 onOptionsItemSelected 中调用 startSearch.尝试从 onOptionsItemSelected 中删除它,看看会发生什么.

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. Try removing that from onOptionsItemSelected and see what happens.

经过进一步研究,看起来您可能已经尝试实现 此处,但只需要其中一个(尽管一个只适用于 Honeycomb 及以上版本,除非您使用的是 ABS 或 AppCompat).

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

这篇关于Android 搜索界面——显示多个搜索视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆