2017-07-12 92 views
-3

我做了片段,我想顯示點擊以發佈菜單。但菜單不顯示。我不知道沒有表現出問題。現在我想爲下一步程序顯示菜單請幫助我。菜單沒有顯示在片段

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> 

謝謝你幫助我:)

+0

加上'onCreateOptionsMenu'在父活動,而在片段 –

回答

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

刪除,但沒有表現出@MayurRaval – ThLez

+0

添加您現在'menu_post'xml –

+0

編輯問題XML代碼@MayurRaval – ThLez

0

寫超級方法開始

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

不工作@AmrishKakadiya – ThLez

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; 
} 
+0

我嘗試編輯,但不工作@Anil – ThLez

+0

請檢查我的答案,或再次發送您的Java代碼讓我看看 – Anil

+0

到哪裏發送@Anil – ThLez

0

不要放置工具欄裏面的RelativeLayout。

請將工具欄放置在LinearLayout中。它會解決問題。