How to make SearchView always expanded in android?

后端 未结 4 1671
攒了一身酷
攒了一身酷 2021-01-01 08:56

I have SearchView in the top of the layout (not in the action bar), is there anyway to force this View to be always expanded (opened)?

If n

4条回答
  •  庸人自扰
    2021-01-01 08:59

    I am doing this into fragment , i done it by using

    searchItem.expandActionView();
    

    but when user press back button it collapse so in collapse method i used

    getActivity().getSupportFragmentManager().popBackStack();
    // when it collapsed i am going back
    

    Below is my solution in detail:

    search_menu.xml

    
    
        
        
    
    

    in fragment i used below code :

    @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            inflater.inflate(R.menu.search_menu, menu);
    
            // Define the listener
            MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionCollapse(MenuItem item)
                {
                    getActivity().getSupportFragmentManager().popBackStack();
                    // Do something when action item collapses
                    return true;  // Return true to collapse action view
                }
    
                @Override
                public boolean onMenuItemActionExpand(MenuItem item)
                {
    
                    // Do something when expanded
                    return true;  // Return true to expand action view
                }
            };
    
    
    
            MenuItem searchItem = menu.findItem(R.id.search_contacts);
    
    
            // Assign the listener to that action item
            MenuItemCompat.setOnActionExpandListener(searchItem, expandListener);
    
            // Any other things you have to do when creating the options menu…
    
    
    
            SearchView searchView =
                    (SearchView) MenuItemCompat.getActionView(searchItem);
            //searchView.setIconifiedByDefault(false);
            searchItem.expandActionView(); // when fragment opens it expanded auto
    
    
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }
    
                @Override
                public boolean onQueryTextChange(String newText)
                {
                    seachViewFunction(newText); // in searchViewFunction(newText); i am doing my things
                    return false;
                }
            });
    
            super.onCreateOptionsMenu(menu, inflater);
        }
    

提交回复
热议问题