我做了片段,我想顯示點擊以發佈菜單。但菜單不顯示。我不知道沒有表現出問題。現在我想爲下一步程序顯示菜單請幫助我。菜單沒有顯示在片段
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>
謝謝你幫助我:)
加上'onCreateOptionsMenu'在父活動,而在片段 –