将ActionBar搜索查询传递给片段 [英] Pass ActionBar search query to fragment
本文介绍了将ActionBar搜索查询传递给片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这里可以看到我有一个SHERLOCK FRAGMENT ACTIVITY
,其中包含四个FRAGMENTS
和一个SEARCH VIEW
.有4个片段,其中最后一个是FRAGMENT SEARCH RESULTS
Here is the seen I have a SHERLOCK FRAGMENT ACTIVITY
which holds four FRAGMENTS
and a SEARCH VIEW
. There are 4 fragments in which last is FRAGMENT SEARCH RESULTS
我的问题是如何将搜索查询的数据从搜索视图传递到FRAGMENT SEARCH RESULTS
并在FRAGMENT SEARCH RESULTS
My question is how to pass data of search query to FRAGMENT SEARCH RESULTS
from search view and display search result in FRAGMENT SEARCH RESULTS
我实现了
private void setupSearchView(MenuItem searchItem) {
if (isAlwaysExpanded()) {
mSearchView.setIconifiedByDefault(false);
} else {
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
mSearchView.setOnQueryTextListener(this);
}
public boolean onClose() {
return false;
}
protected boolean isAlwaysExpanded() {
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
if (query.length() > 0) {
**//WHAT SHOULD I WRITE HERE**
}
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
推荐答案
@Override
public boolean onQueryTextSubmit(String query) {
if (query.length() > 0) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment newFragment = new SearchFragment(); //your search fragment
Bundle args = new Bundle();
args.putString("query_string", query);
newFragment.setArguments(args);
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
return false;
}
这篇关于将ActionBar搜索查询传递给片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文