菜单未显示在片段中

时间:2017-07-12 05:24:00

标签: android android-fragments

我制作片段,我想显示点击发布的菜单。但菜单没有显示。我不知道没有显示的问题。现在我想显示下一步计划的菜单请帮助我。

    public class FragmentViewPost extends Fragment {

    public static FragmentViewPost newInstance() {
        FragmentViewPost fragment = new FragmentViewPost();
        return fragment;
    }


    public FragmentViewPost() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setHasOptionsMenu(true);
        View view = inflater.inflate(R.layout.fragment_fragment_view_post, container, false);
        return view;
    }

    private RecyclerView mBlogList;
    private DatabaseReference mDatabase;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mAuth = FirebaseAuth.getInstance();
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

                if(firebaseAuth.getCurrentUser() == null){


                    startActivity();

                }
            }
        };

        mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
        mBlogList = (RecyclerView) view.findViewById(R.id.blog_list_fragment);
        mBlogList.setHasFixedSize(true);
        mBlogList.setLayoutManager(new LinearLayoutManager(getContext()));

    }

    private void startActivity() {

        Intent loginIntent = new Intent(getActivity(),PageLogin.class);
        loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(loginIntent);

    }


    @Override
    public void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(mAuthListener);

        FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(

                Blog.class, R.layout.blog_row, BlogViewHolder.class, mDatabase

        ) {
            @Override
            protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {

                viewHolder.setTitle(model.getTitle());
                viewHolder.setDesc(model.getDesc());
                viewHolder.setImage(getActivity(), model.getImage());
                viewHolder.setUsername(model.getUsername());

            }
        };

        mBlogList.setAdapter(firebaseRecyclerAdapter);
    }



    public static class BlogViewHolder extends RecyclerView.ViewHolder{

        View mView;

        public BlogViewHolder(View itemView) {
            super(itemView);

            mView = itemView;
        }

        public void setTitle(String title){

            TextView post_title = (TextView) mView.findViewById(R.id.post_title);
            post_title.setText(title);

        }

        public void setDesc(String desc){

            TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
            post_desc.setText(desc);

        }

        public void setImage(Context ctx, String image){

            ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
            Picasso.with(ctx).load(image).into(post_image);

        }

        public void setUsername(String username){

            TextView post_username = (TextView) mView.findViewById(R.id.post_username);
            post_username.setText(username);

        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//        super.onCreateOptionsMenu(menu,inflater);
        inflater.inflate(R.menu.menu_post, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == R.id.action_add){

            goToAttract();

//        switch (item.getItemId()) {
//            case R.id.action_add:
//                // Not implemented here
//                Log.d("=============>>>","search1");
//                return false;
//            case R.id.action_adduser:
//                Log.d("=============>>>","scan1");
//                return true;
//            default:
//                break;

        }

        return super.onOptionsItemSelected(item);

//        return false;
    }

    private void goToAttract() {

        Intent intent = new Intent(getActivity(), Post.class);
        startActivity(intent);

    }

和xml代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/action_add"
        android:icon="@mipmap/ic_add_white_24dp"
        android:title="Post"
        app:showAsAction="always"/>



</menu>

感谢您的帮助:)

4 个答案:

答案 0 :(得分:1)

删除menu.clear()

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_post, menu);
        super.onCreateOptionsMenu(menu,inflater);
        menu.clear(); // Remove this line
    }

在您推送此片段的活动中

实施此方法

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu,inflater);
        inflater.inflate(R.menu.menu_post, menu);    
    }

中得到它
 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add:
                  // pass data to fragment
              break;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

答案 1 :(得分:1)

试试这个为我工作

@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

在此之后

@Override
    public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
        inflater.inflate(R.menu.menu_activity_categories, menu);

    }

并添加此

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_search:
            // Not implemented here
            Log.d("=============>>>","search1");
            return false;
        case R.id.action_scanning:
            Log.d("=============>>>","scan1");
            return true;
        default:
            break;
    }

    return false;
}

答案 2 :(得分:0)

在启动时编写超级方法

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu,inflater);
        inflater.inflate(R.menu.menu_post, menu);
    }

答案 3 :(得分:0)

不要将工具栏放在relativelayout内。

请将工具栏放在LinearLayout中。它将解决问题。