在片段中添加onOptionsItemSelected调用 [英] Add onOptionsItemSelected calling in Fragment
本文介绍了在片段中添加onOptionsItemSelected调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_filter) {
FragmentManager fm = getSupportFragmentManager();
if (userType.equals("İş Arayan"))
filterDialogTitle = "İş İlanları Filtre";
else if (userType.equals("Hizmet Arayan"))
filterDialogTitle = "Hizmet İlanları Filtre";
FilterDialogFragment editNameDialogFragment = FilterDialogFragment.newInstance(filterDialogTitle);
editNameDialogFragment.show(fm, "fragment_edit_name");
return true;
}
return super.onOptionsItemSelected(item);
}
我在Fragment中添加了,但是我没有打电话,如果我在MainActivity中添加,它可以工作,但是我想在Fragment中调用.我该怎么办?
I added in Fragment, but i didn' t called, if i add in MainActivity, it works but i want call in Fragment. How can i do this ?
推荐答案
在 Fragment 中,您必须调用setHasOptionsMenu(true)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
...
}
然后假设您必须处理menu_item_to_handle_in_fragment
项目单击
Then suppose you have to handle menu_item_to_handle_in_fragment
item click
对于片段类
For Fragment class
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_to_handle_in_fragment:
// Do onlick on menu action here
return true;
}
return false;
}
对于活动类
For Activity class
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_to_handle_in_fragment:
return false;
}
return false;
}
这篇关于在片段中添加onOptionsItemSelected调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文