SearchView和BackPress事件处理

时间:2016-12-28 07:08:37

标签: android android-fragments searchview onbackpressed

我在SearchView中添加了HomeScreen。我使用SearchView进行地点搜索。这是我HomeScreen的截图: enter image description here

这是我在onCreateOptionsMenu方法中的代码:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.home, menu);
    searchItem = menu.findItem(R.id.menu_item_search_places);
    searchItem.setIcon(FontIconDrawable.inflate(getContext(), R.xml.ic_search));
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnSuggestionListener(this);
    searchView.setOnQueryTextListener(this);
    placesSearchAdapter = new PlacesSearchAdapter(this.getContext());
    searchView.setSuggestionsAdapter(placesSearchAdapter);
    searchView.setMaxWidth(750);
    MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            corLDriveInfoContainer.setVisibility(View.GONE);
            fabDrivingDirection.setVisibility(View.VISIBLE);
            mapView.deleteAllAnnotationsAndCustomPOIs();
            return true;
        }
    });
}

当我点击SearchView时,这是我SearchIconenter image description here

这是我的SearchView结果:

下进行。enter image description here

我有FabButton来推动所选位置。

d。enter image description here

但是,我的问题是,当我按下按钮时,如何从View C返回View D

2 个答案:

答案 0 :(得分:1)

您应该像这样覆盖活动中的onBackPressed方法

@Override
public void onBackPressed() {
   super.onBackPressed();
   //change your View from D to C using the same code you used to go to D from C
}

答案 1 :(得分:1)

我将其包含在onBackPress方法中解决我的问题:

@Override
public void onBackPressed() {
     searchItem.expandActionView();
     searchView.setQuery("", false);
     searchView.clearFocus();

  super.onBackPressed();
}