从片段更改ActionBar标题

从片段更改ActionBar标题

本文介绍了从片段更改ActionBar标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有4个片段

问题

在每个片段之间跳转时,没有适当的标题显示.

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

我创建了一个gif来显示:

I have created a gif to show that:

标题应与每个片段中的文字相同.

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

我的 MainActivity 看起来像这样:

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

推荐答案

我不知道这样做是否合适,但是您可以将页面更改侦听器添加到viewpager中,并且可以在onPageSelected方法中更改标题:

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) {

            }
        });

这篇关于从片段更改ActionBar标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:45