[英]How to add a SearchView into a navigation drawer?
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
我想将
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>
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. 我认为在
Activity
使用ActionBar
或ToolBar
来添加SearchView
将是一个很好的解决方案。 You can try like this: 你可以尝试这样:
First, add SearchView
into your Menu
: 首先,将
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>
Second, add actions on your SearchView
: 其次,在
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 . 而且,如果您想了解更多详情,请点击此处 。
ExpandableListView
cannot have any children added – these are managed by the adapter. ExpandableListView
不能添加任何子项 - 这些子项由适配器管理。 You can add the SearchView
as a header to the adapter if you want it to be part of the list. 如果希望将
SearchView
作为列表的一部分,可以将SearchView
作为标头添加到适配器。 But implementing the drawer as a fragment that will hold both SearchView
and an ExpandableListView
would be a better solution. 但实现抽屉作为一个片段,将包含
SearchView
和ExpandableListView
将是一个更好的解决方案。
You can set a fragment for your navigation drawer, and set the custom view to the fragment, 您可以为导航抽屉设置片段,并将自定义视图设置为片段,
<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>
This will help you simplify your code structure, and here is a good tutorial for Expandable list with search view that you add it to your drawer frgment http://www.mysamplecode.com/2012/11/android-expandablelistview-search.html 这将有助于您简化代码结构,这是一个很好的可扩展列表教程,带有搜索视图,您可以将其添加到抽屉文件中http://www.mysamplecode.com/2012/11/android-expandablelistview-search.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.