Android 如何将SearchView添加到导航抽屉中?
我开发了一个具有可扩展列表视图的导航抽屉,如下所示Android 如何将SearchView添加到导航抽屉中?,android,android-layout,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"&g
<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>
我犯了个错误
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
谢谢,ExpandableListView不能添加任何子项–这些子项由适配器管理。如果希望将
SearchView
作为列表的一部分,可以将其作为标题添加到适配器中。但是将抽屉实现为一个片段,该片段将同时包含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-->
<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>
我认为在活动中使用操作栏
或工具栏
添加搜索视图
将是一个很好的解决方案。您可以这样尝试:
首先,将SearchView
添加到菜单中:
<?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>
如果你想知道更多细节,请查看
<?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>
@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);
}