Android 操作栏项目可以单击,但不响应片段中的事件
基本上,我的项目必须有不同的工具栏,一个用于底部导航栏切换的每个片段。 片段工具栏项目是可点击的,当我长按时,显示标题项目等,但它们不执行操作。例如,当我设置在单击该项目时显示祝酒词时,他将不会执行。有什么建议吗?谢谢Android 操作栏项目可以单击,但不响应片段中的事件,android,android-fragments,android-actionbar,android-toolbar,Android,Android Fragments,Android Actionbar,Android Toolbar,基本上,我的项目必须有不同的工具栏,一个用于底部导航栏切换的每个片段。 片段工具栏项目是可点击的,当我长按时,显示标题项目等,但它们不执行操作。例如,当我设置在单击该项目时显示祝酒词时,他将不会执行。有什么建议吗?谢谢 public class FeedFragment extends Fragment { public static final String TAG = "Tag Free"; DialogFragment mDialog; FloatingActi
public class FeedFragment extends Fragment {
public static final String TAG = "Tag Free";
DialogFragment mDialog;
FloatingActionButton fab;
RevealFrameLayout reveal;
FrameLayout frame;
private ConstraintLayout layoutDialog;
boolean isOpen = false;
public FeedFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_feed, container, false);
/** Toolbar **/
Toolbar myToolbar = (Toolbar) view.findViewById(R.id.my_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(myToolbar);
ActionBar ab = ((AppCompatActivity) getActivity()).getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(false);
// navigation bottom
fab = getActivity().findViewById(R.id.fab_button);
fab.setImageDrawable(getResources().getDrawable(R.drawable.ic_pencil_black_24dp));
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction ft = getFragmentManager().beginTransaction().setCustomAnimations(R.anim.hold, R.anim.slide_down);
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment dialogFragment = new PostDialog();
dialogFragment.show(ft, "dialog");
dialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentTheme);
}
});
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.appbar_feed, menu);super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_personProfile:
Toast.makeText(getActivity(),"not responding",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), MyUserProfileActivity.class);
startActivity(intent);
return true;
case R.id.action_settings:
Toast.makeText(getActivity(),"not responding",Toast.LENGTH_SHORT).show();
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
}
您没有链接到活动方法中的超类。 重构代码,如下所示:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.appbar_feed, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_personProfile:
Toast.makeText(getActivity(),"not responding",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), MyUserProfileActivity.class);
startActivity(intent);
break;
case R.id.action_settings:
Toast.makeText(getActivity(),"not responding",Toast.LENGTH_SHORT).show();
break;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
break;
}
return super.onOptionsItemSelected(item);
}
希望现在您的代码可以正常工作 仍然无法使用“编辑”。可能是我的活动中有什么,不是吗?你能在日志中记录一些,看看是不是触发了。让我知道。您能为您的特定开关情况调试is item.getItemId()吗?我已经完成了,查看日志始终是默认选项。