简体   繁体   English

ActionBar标题不显示特定片段

[英]ActionBar Title not show for specific fragments

I've 4 different fragments, however it doesn't show the title for any of the fragments. 我有4个不同的片段,但是没有显示任何片段的标题。 When i created the fragments, everything was working fine, however now no title shows up on the actionBar. 当我创建片段时,一切工作正常,但是现在actionBar上没有标题。 I've done all the changes which i found on the stackoverflow, but non of them worked for me 我已经完成了我在stackoverflow上发现的所有更改,但是没有一个对我有用

the code for my main activity is :- 我主要活动的代码是:-

 private ActionBar toolbar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = getSupportActionBar();
    toolbar.setTitle("Home");
    loadFragment(new home());

    BottomNavigationView navigation = (BottomNavigationView)findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

}

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        Fragment fragment;
        switch (menuItem.getItemId()){

            case R.id.navigation_home:
                toolbar.setTitle("Home");
                fragment = new home();
                loadFragment(fragment);
                return true;

            case R.id.navigation_dashboard:
                toolbar.setTitle("Dashboard");
                fragment = new Dashboard();
                loadFragment(fragment);
                return true;

            case R.id.navigation_notifications:
                toolbar.setTitle("Notifications");
                fragment = new Notification();
                loadFragment(fragment);
                return true;

            case R.id.navigation_profile:
                toolbar.setTitle("Profile");
                fragment = new Profile();
                loadFragment(fragment);
                return true;
        }

        return false;
    }
};

private void loadFragment(Fragment fragment){

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_container, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

the code for one of the fragments which contains widgets(Rest of them are yet empty) is below :- 包含小部件的片段之一的代码(其余片段仍为空)如下:

private OnFragmentInteractionListener mListener;

Button AskForHelp, Drafts, LogOut, Settings;

View view;

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

public static Dashboard newInstance(String param1, String param2) {
    Dashboard fragment = new Dashboard();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_dashboard, container,false);

    AskForHelp = (Button)view.findViewById(R.id.askHelp);
    AskForHelp.setOnClickListener(this);

    Drafts = (Button)view.findViewById(R.id.drafts);
    Drafts.setOnClickListener(this);

    LogOut = (Button)view.findViewById(R.id.logOut);
    LogOut.setOnClickListener(this);

    Settings = (Button)view.findViewById(R.id.settings);
    Settings.setOnClickListener(this);

    return view;

}

public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

@Override
public void onClick(View v) {

    switch (v.getId()){

        case R.id.askHelp:
            Intent intent = new Intent(getContext(), Questions.class);
            startActivity(intent);
            break;

        case R.id.drafts:
            Toast.makeText(getContext(),"Function not enabled", Toast.LENGTH_SHORT).show();
            break;

        case R.id.logOut:
            Toast.makeText(getContext(),"Function not enabled", Toast.LENGTH_SHORT).show();
            break;

        case R.id.settings:
            Toast.makeText(getContext(),"Function not enabled", Toast.LENGTH_SHORT).show();
            break;
    }

}

public interface OnFragmentInteractionListener {
    void onFragmentInteraction(Uri uri);
}

I've tried almost everything mentioned below. 我已经尝试了下面提到的几乎所有内容。 But it's still the same 但它仍然是一样的

In fragment just put this line in onCreatView so it will set your fragment title each time when you load your fragment. 在片段中,只需将此行放在onCreatView这样每次加载片段时都会设置片段标题。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    getActivity().setTitle("you title here");
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM