How to add SearchView to navigation box?

I am creating an expandable list navigation box like this

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:id="@+id/map" tools:context=".HomeActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

    <!-- The navigation drawer-->
    <ExpandableListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"
        android:listSelector = "@drawable/selector_list_item">
    </ExpandableListView>
</android.support.v4.widget.DrawerLayout>

I want to add SearchViewto the navigation box, so I’ll add it toExpandableListView

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:id="@+id/map" tools:context=".HomeActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

    <!-- The navigation drawer-->
    <ExpandableListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"
        android:listSelector = "@drawable/selector_list_item">
        <SearchView android:id="@+id/sv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </ExpandableListView>
</android.support.v4.widget.DrawerLayout>

I got an error

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

thank

+4
source share
3 answers

I think using ActionBareither ToolBarin yours Activityto add SearchViewwould be a good solution. You can try the following:

First add SearchViewto your Menu:

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

Second, add actions to SearchView:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_activity_actions, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    // Configure the search info and add any event listeners
    ...
    return super.onCreateOptionsMenu(menu);
}

And if you want to know more information, check here .

+4
source

ExpandableListView - . SearchView , , . , SearchView, ExpandableListView, .

+1

,

 <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<!-- The main content view -->
     <fragment 
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent" 
       android:id="@+id/map"        
       tools:context=".HomeActivity"
       android:name="com.google.android.gms.maps.SupportMapFragment" />

<!-- The navigation drawer-->

    <fragment
       android:id="@+id/menufragment"
       android:name="com.example.fragments.MenuDrawer"
       android:layout_width="match_parent"
       android:layout_gravity="start"
       android:layout_height="match_parent" />
  </android.support.v4.widget.DrawerLayout>

, , frage http://www.mysamplecode.com/2012/11/android-expandablelistview-search.html

+1

All Articles