How to add a functional search bar to a navigation drawer?

拟墨画扇 提交于 2020-01-06 19:42:19

问题


I am developing a wallpaper app that loads it's images from Picasa I have a Navigation drawer that retrieves it's items name from the Picasa album names (I am using the source code of this tutorial Link ), so I want to add a search bar that filters the items in the navigation drawer, any idea on how I could accomplish this?


回答1:


It might be a little late. But may help others.

  1. Add an EditText to navigation Drawer.(activity_main.xml)

    <EditText
       android:id="@+id/inputSearch"
       android:layout_width="match_parent"
       android:layout_height="40dp"
       android:layout_margin="10dp"
       android:hint="search" 
       android:padding="10dp"
       android:layout_marginTop="25dp"
       android:inputType="text" >
     </EditText>
    
  2. add a TextWatcher to editText. (Add the following lines to onCreate)

       final EditText inputSearch = (EditText) findViewById(R.id.inputSearch);
    
        inputSearch.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
            //You should use the adapter in NavigationDrawerFragment
            NavigationDrawerFragment.adapter.getFilter().filter(cs);
    
        }
    
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
    
        }
    });
    

Hope this helps.




回答2:


You can make your navigation drawer a Fragment that contains an ArrayAdapter, which implements Filterable. This adapter would contain list of your Picasa album names.

For the search box, a simple EditText would do the job. Register a TextWatcher that would listen to text change events and trigger Filter.filter() on a Filter returned from your ArrayAdapter.getFilter() with input text as query.



来源:https://stackoverflow.com/questions/28733957/how-to-add-a-functional-search-bar-to-a-navigation-drawer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!