How to add a functional search bar to a navigation

2019-09-05 07:41发布

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?

2条回答
▲ chillily
2楼-- · 2019-09-05 08:13

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-09-05 08:35

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.

查看更多
登录 后发表回答