操作栏项目是可单击的,但不响应片段中的事件

时间:2018-08-06 16:01:51

标签: android android-fragments android-actionbar android-toolbar

基本上,我的项目必须具有不同的工具栏,每个工具栏分别由底部导航栏切换。 片段工具栏项目是可单击的,当我长按时,会显示标题项目等,但是它们不执行操作。例如,当我设置要在单击该项目时显示一个吐司时,他将不会执行。有什么建议吗?谢谢。

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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您没有在活动方法中链接到超类。

按如下所示重构代码:

@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);
}

希望现在您的代码可以正常工作。