如何将SearchView添加到导航抽屉中?

时间:2015-05-16 17:51:43

标签: android android-layout

我开发了一个带有可扩展列表视图的导航抽屉,就像这样

<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>

我想在导航抽屉中添加SearchView,因此我将其添加到ExpandableListView

<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>

我收到了错误

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

由于

3 个答案:

答案 0 :(得分:6)

我认为在ActionBar中使用ToolBarActivity添加SearchView将是一个很好的解决方案。你可以尝试这样:

首先,将SearchView添加到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>

其次,在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);
}

如果您想了解更多详情,请查看here

答案 1 :(得分:1)

ExpandableListView无法添加任何子项 - 这些子项由适配器管理。如果您希望它成为列表的一部分,可以将SearchView作为标头添加到适配器。但是将抽屉实现为可以同时包含SearchViewExpandableListView的片段将是更好的解决方案。

答案 2 :(得分: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>

这将有助于您简化代码结构,这是一个很好的可扩展列表教程,带有搜索视图,您可以将其添加到抽屉文件http://www.mysamplecode.com/2012/11/android-expandablelistview-search.html