如何将SearchView添加到导航抽屉中? [英] How to add a SearchView into a navigation drawer?

查看:55
本文介绍了如何将SearchView添加到导航抽屉中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<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

谢谢

我认为在您的Activity中使用ActionBarToolBar来添加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);
}

并且,如果您想了解更多详细信息,请在此处

I develop a navigation drawer with expandable list view, 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 a SearchView into the navigation drawer, so I add it into the 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>

I got a error

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

Thanks

解决方案

I think using ActionBar or ToolBar in your Activity to add a SearchView would be a good solution. You can try like this:

First, add SearchView into 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 on your 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 details, check here.

这篇关于如何将SearchView添加到导航抽屉中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆