简体   繁体   中英

Change ActionBar title from Fragment

I have 4 fragments

ISSUE

there is no proper title displayed in each fragment when I am jumping between them.

I have created a gif to show that: http://i.imgur.com/QFTdH3w.gif

Titles should be the same as a text in each fragment.

My MainActivity looks like this:

public class MainActivity extends AppCompatActivity {  
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new InfoFragment(), "INFO");
        adapter.addFragment(new NavFragment(), "NAV");
        adapter.addFragment(new PetrolFragment(), "PETROL");
        adapter.addFragment(new CalcFragment(), "CALC");
        viewPager.setAdapter(adapter);
    }

    public void setActionBarTitle(String title) {
        getSupportActionBar().setTitle(title);
    }

Each of my fragments looks like this:

public class InfoFragment extends Fragment {

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((MainActivity) getActivity()).setActionBarTitle("INFO"); // here are other names according to each fragment
    }

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

        return inflater.inflate(R.layout.fragment_info, container, false);
    }
}

I don't know if it's a proper way to do that but you can add a page change listener to your viewpager and in onPageSelected method you can change your title:

yourViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                // change your title
                // inflate menu
                // customize your toolbar
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

Try the following code

For Fragment title :

getActivity().getActionBar().setTitle("Fragment1");

Add this lines to all your Fragments onCreate or OnCreateView method

((MainActivity) getActivity()).setActionBarTitle("Your title");

by Default set a fragment in MainActivity

viewPager.setCurrentItem(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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