【发布时间】:2015-09-23 06:26:22
【问题描述】:
我正在尝试在片段中以独立的Toolbar 实现SearchView,但无法获得任何结果。
我关注了这个答案SearchView at a secondary Toolbar,但没有成功。
这是我的实现
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_tab_fragment3, container, false);
initViews(rootView);
initBottomToolbar(rootView);
return rootView;
}
private void initBottomToolbar(View rootView) {
mToolbarBottom = (Toolbar) rootView.findViewById(R.id.toolbar_bottom);
// Set an OnMenuItemClickListener to handle menu item clicks
mToolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
// Handle the menu item
Toast.makeText(getActivity(), "action_search clicked", Toast.LENGTH_SHORT).show();
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (menuItem != null) {
Log.d("Bottom Toolbar", "menuItem != null");
searchView = (SearchView) menuItem.getActionView();
}
if (searchView != null) {
Log.d("Bottom Toolbar", "searchView != null");
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getActivity(), query, Toast.LENGTH_SHORT).show();
Log.e("Bottom Toolbar", "Submitted: "+query);
return false;
}
@Override
public boolean onQueryTextChange(String newQuery) {
Toast.makeText(getActivity(), newQuery, Toast.LENGTH_SHORT).show();
Log.d("Bottom Toolbar", "Changed: " + newQuery);
List<ContactsBean> filteredContactsBeanList = filter(mContactsBeanList, newQuery);
mContactsAdapter.animateTo(filteredContactsBeanList);
mContactsRecyclerView.scrollToPosition(0);
return true;
}
});
}
return true;
}
});
// Inflate a menu to be displayed in the toolbar
mToolbarBottom.inflateMenu(R.menu.menu_bottom_toolbar);
}
即使我点击SearchView,它既不显示Toast,也不在日志中显示任何消息。
【问题讨论】:
-
v4all123.blogspot.in/2015/08/… 可能会对您有所帮助。
-
@Gunaseelan 这是用于主工具栏的。我有 2 个 Toolbars ,一个在活动中,另一个在片段中。
标签: android android-fragments android-5.0-lollipop searchview android-toolbar