我有一个可扩展的名单。我可以通过SearchView
小部件和SearchView.OnQueryTextListener, SearchView.OnCloseListener
实现搜索。我想用
<searchable> ... </searchable>
配置。我想附加与SearchView相同类型的侦听器来获取查询和过滤数据。如何使用<searchable>
我有以下文件。
的manifest.xml
<activity
android:name=".ContactListActivity"
android:label="@string/app_name"
android:theme="@style/Theme.MyCompatTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
saved_tabs.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_marginLeft="16dp" android:layout_marginRight="16dp">
<!--<SearchView android:id="@+id/search" android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content" android:layout_alignParentRight="true"-->
<!--android:layout_alignParentTop="true" android:iconifiedByDefault="false" />-->
<ExpandableListView android:id="@+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:dividerHeight="1dp"
android:indicatorLeft="300dp"
/>
</LinearLayout>
searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:label="@string/app_name"
android:hint="@string/search_hint" />
options_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools">
<item
android:id="@+id/action_settings"
android:title="@string/app_name"
android:orderInCategory="100"
app:showAsAction="never"/>
<item
android:id="@+id/menu_search"
android:title="@string/app_name"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always"/>
</menu>
ContactListActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
和
SavedTabFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.saved_tabes, null);
ExpandableListView elv = (ExpandableListView) v.findViewById(R.id.list);
Map<String,Object> contactDetails = getContacts();
final List<String> names = (List<String>)contactDetails.get("names");
final List<List<String>> numbers = (List<List<String>>)contactDetails.get("numbers");
expandableListAdapter = new MyExpandableListAdapter(names, numbers,getActivity());
elv.setAdapter(expandableListAdapter);
return v;
}