Adding SearchView in Fragment

前端 未结 7 636
误落风尘
误落风尘 2020-12-14 02:36

Trying to include a searchview in actionbar. For this, I have done the following:

Created MenuSearch.xml in the menu folder as given below:



        
相关标签:
7条回答
  • 2020-12-14 02:59

    Change your MenuSearch.xml to

        <?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
            <item android:id="@+id/action_search"
                android:title="@string/app_name"
                android:icon="@drawable/ic_action_search"
                app:showAsAction="collapseActionView|ifRoom"
                 />
        </menu>
    

    And in your fragment, add searchView.setOnQueryTextListener

        @Override
            public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
                super.onCreateOptionsMenu(menu, inflater);
                menu.clear();
                inflater.inflate(R.menu.search_option_menu, menu);
                MenuItem item = menu.findItem(R.id.action_search);
                SearchView searchView = new SearchView(((MainActivity) mContext).getSupportActionBar().getThemedContext());
               // MenuItemCompat.setShowAsAction(item, //MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | //MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
               //  MenuItemCompat.setActionView(item, searchView);
               // These lines are deprecated in API 26 use instead
     item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
            item.setActionView(searchView);
                searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        return false;
                    }
                    @Override
                    public boolean onQueryTextChange(String newText) {
                        return false;
                    }
                });
                searchView.setOnClickListener(new View.OnClickListener() {
                                                  @Override
                                                  public void onClick(View v) {
    
                                                  }
                                              }
                );
            }
    
    0 讨论(0)
  • 2020-12-14 03:01

    Answer updated for 2018. Sorry for Kotlin, if you're using Java.

    Your xml menu resource (search_menu.xml for example):

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item android:id="@+id/action_search"
            android:title="@string/app_name"
            android:icon="@drawable/ic_search"
            app:showAsAction="collapseActionView|ifRoom"
            />
    </menu>
    

    Build-up code:

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater?) {
        super.onCreateOptionsMenu(menu, inflater)
        menu.clear()
        inflater?.inflate(R.menu.search_menu, menu)
        val searchView = SearchView((context as MainActivity).supportActionBar?.themedContext ?: context)
        menu.findItem(R.id.action_search).apply {
            setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW or MenuItem.SHOW_AS_ACTION_IF_ROOM)
            actionView = searchView
        }
    
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String): Boolean {
                return false
            }
    
            override fun onQueryTextChange(newText: String): Boolean {
                return false
            }
        })
        searchView.setOnClickListener {view ->  }
    }
    

    You'll probably also need to put this line in onCreate() (worked for my fragment) to get this all to appear:

    setHasOptionsMenu(true)
    
    0 讨论(0)
  • 2020-12-14 03:03

    For late 2018 and for apis >= 26 compatible (deprecation warnings) and a properly formatted answer:

    The menu.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item android:id="@+id/action_search"
            android:title="Search_change_me_with_proper_id"
            android:icon="@drawable/ic_action_search"
            app:showAsAction="collapseActionView|ifRoom"/>
    </menu>
    

    Java code inside the ActivityFragment:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setHasOptionsMenu(true); // Add this!
        return inflater.inflate(R.layout.fragment_add_app, container, false);
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
    
        inflater.inflate(R.menu.menu_add_app, menu);
        MenuItem item = menu.findItem(R.id.action_search_menu_add_app);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
    
        SearchView searchView = (SearchView) item.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
    
            @Override
            public boolean onQueryTextChange(String newText) {
                // Here is where we are going to implement the filter logic
                return true;
            }
    
        });
    }
    

    OR as a second option you let the ActivityFragment implement SearchView.OnQueryTextListener and then use the menu search:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setHasOptionsMenu(true); // Add this! (as above)
        return inflater.inflate(R.layout.fragment_add_app, container, false);
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.menu_add_app, menu);
        MenuItem item = menu.findItem(R.id.action_search_menu_add_app);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
    
        SearchView searchView = (SearchView) item.getActionView();
        searchView.setOnQueryTextListener(this); // Fragment implements SearchView.OnQueryTextListener
    }
    
    @Override
    public boolean onQueryTextChange(String query) {
        // Here is where we are going to implement the filter logic
        return false;
    }
    
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }
    
    0 讨论(0)
  • 2020-12-14 03:05

    i solved this issue i wants to share my answer for searchView NullPointerException

    we need to remove below method from Main Activity

          public boolean onOptionsItemSelected(MenuItem item) {
              int id = item.getItemId();  
              return true;
    

    and use inside fragment in OnActivityCreated setHasOptionsMenu(true) and Override methods

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
             inflater.inflate(R.menu.menu, menu);
        }
    
        @Override
         public boolean onOptionsItemSelected(MenuItem item) {
           // handle item selection
            switch (item.getItemId()) {
                case R.id.action_search:
    
         //       onCall();   //your logic
    
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    0 讨论(0)
  • 2020-12-14 03:17
    search_view.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
                override fun onQueryTextSubmit(query: String?): Boolean {
                    return false
                }
    
                override fun onQueryTextChange(query: String?): Boolean {
                    adapter.filter.filter(query)
                    return true
                }
            })
    

    And don't add the intent filter for your activity in Manifest file.

    0 讨论(0)
  • 2020-12-14 03:23

    Your search_options_menu.xml should look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item android:id="@+id/action_search"
            android:title="@string/app_name"
            android:icon="@drawable/ic_action_search"
            app:showAsAction="collapseActionView|ifRoom"
             />
    </menu>
    

    Override onCreateOptionMenu method in your fragment setHasOptionsMenu(true); inside onCreate() on your fragment.

    @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            super.onCreateOptionsMenu(menu, inflater);
           menu.clear();
            inflater.inflate(R.menu.search_option_menu, menu);
            MenuItem item = menu.findItem(R.id.action_search);
                    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
            MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
            MenuItemCompat.setActionView(item, searchView);
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }
                @Override
                public boolean onQueryTextChange(String newText) {
                        //Do search code here
                    return true;
                }
            });
    
        }
    
    0 讨论(0)
提交回复
热议问题