选择建议后,Android Searchview禁用全屏无效

时间:2016-04-03 19:27:06

标签: android searchview landscape-portrait android-fullscreen

场合

我的工具栏中有一个SearchView,可以在用户输入搜索时显示建议。最初,在横向方向上,一切正常,显示的软键盘和搜索视图可见。但是,在用户选择建议并尝试进行第二次搜索之后,搜索界面现在处于全屏状态,其中搜索视图被覆盖并且无法看到建议。

问题

无论使用多少次搜索功能,如何无法全屏显示?

我尝试过的事情

ImeOptions - flagNoFullscreen,flagNoExtractUi

代码

MainFragment

Array.prototype.compact = ->
  [v for v in @ when v and (typeof v.isempty isnt \function or not v.isempty!) and (typeof v isnt \object or Object.keys value .length > 0)]

2 个答案:

答案 0 :(得分:1)

问题似乎是调用SearchView的onActionViewCollapsed。我通过在menu.xml中添加collapseActionView来解决了这个问题。

<item
    android:id="@+id/search_restaurant"
    android:title="Search"
    android:icon="@drawable/search"
    android:orderInCategory="2"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always|collapseActionView"/>

然后删除searchview.onActionViewCollapsed。改为添加menuItem.collapseActionView。

searchView.clearFocus();
//searchView.onActionViewCollapsed();
menuItem.collapseActionView(); //Added this
setItemsVisibility(menu, singleSearchItem, true);

答案 1 :(得分:1)

尽量保持简单。我的意思是,你为什么要使用所有requestFocusclearFocus等等。我刚刚创建了一个非常基本的任务实现,一切都像魅力一样。看看:

import android.database.Cursor;
import android.database.MatrixCursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class SearchableActivity extends AppCompatActivity {

    private TextView selectedValueTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchable);
        selectedValueTextView = (TextView) findViewById(R.id.selected_value);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_with_search, menu);
        final MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
        searchView.setSuggestionsAdapter(new SimpleCursorAdapter(
                this,
                android.R.layout.simple_list_item_1,
                null,
                new String[]{"value"},
                new int[]{android.R.id.text1},
                0
        ));
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String query) {
                MatrixCursor cursor = new MatrixCursor(new String[]{BaseColumns._ID, "value"});
                for (int i = 0; i < 10; i++) {
                    cursor.addRow(new Object[]{i, query + i});
                }
                searchView.getSuggestionsAdapter().changeCursor(cursor);
                return true;
            }
        });
        searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
            @Override
            public boolean onSuggestionSelect(int position) {
                return false;
            }
            @Override
            public boolean onSuggestionClick(int position) {
                Cursor cursor = (Cursor) searchView.getSuggestionsAdapter().getItem(position);
                selectedValueTextView.setText(cursor.getString(cursor.getColumnIndex("value")));
                // do this last because collapseActionView calls onQueryTextChange("")
                // which changes cursor data
                searchMenuItem.collapseActionView();
                return false;
            }
        });
        return true;

    }
}