Implementing SearchView in Toolbar with Fragments

后端 未结 1 1547
花落未央
花落未央 2021-02-08 08:21

CURRENT SCENARIO

My app home page consists of navigation drawer, therefore I am having views loaded as fragments. I also have search icon in toolbar. I

1条回答
  •  别那么骄傲
    2021-02-08 09:19

    Put this in your fragment

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setHasOptionsMenu(true);
    }
    

    And you can get the SearchView like follows

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        MenuItem mSearchMenuItem = menu.findItem(R.id.mi_search);
        SearchView searchView = (SearchView) mSearchMenuItem.getActionView();
    }
    

    0 讨论(0)
提交回复
热议问题