选项卡更改时如何更改工具栏标题(查看寻呼机)?

时间:2018-03-10 01:52:08

标签: java android android-viewpager

我正在尝试做一些简单的事情。但我不知道为什么它不起作用。我只想更改toolbar标题以匹配标签更改中的片段。但不知何故,它会得到错误的价值。就像我选择A一样,它会设置标题B.有时标题不会发生变化。我怎样才能做到这一点?谢谢。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = findViewById(R.id.viewpager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        TabLayout tabLayout = findViewById(R.id.tabs);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

        navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    //toolbar.setTitle(R.string.home);
                    FragmentMainHome fragmentMainHome = new FragmentMainHome();
                    return fragmentMainHome;
                case 1:
                    //toolbar.setTitle(R.string.activities);
                    FragmentMainActivities fragmentMainActivities = new FragmentMainActivities();
                    return fragmentMainActivities;
                case 2:
                    //toolbar.setTitle(R.string.people);
                    FragmentMainPeople fragmentMainPeople = new FragmentMainPeople();
                    return fragmentMainPeople;
                case 3:
                    //toolbar.setTitle(R.string.posts);
                    FragmentMainPosts fragmentMainPosts = new FragmentMainPosts();
                    return fragmentMainPosts;
                case 4:
                    //toolbar.setTitle(R.string.media);
                    FragmentMainMedia fragmentMainMedia = new FragmentMainMedia();
                    return fragmentMainMedia;

            }
            return null;
        }

        @Override
        public int getCount() {
            // Show total pages.
            return 5;
        }
    }

5 个答案:

答案 0 :(得分:0)

试试这个

toolbar = (Toolbar) findViewById(R.id.toolbar);            
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("My title");

答案 1 :(得分:0)

有两种方法,您可以在清单中更改 @objc func movieSelected(notification: NSNotification) { NotificationCenter.default.post(name: Notification.Name(NOTIF_MOVIE_SELECTED), object:self) }

中的应用名称
Activity

或者你可以这样编程:

<activity
            android:name=".MainActivity"
            android:label="@string/app_name"  <------ Activity Name
            android:theme="@style/AppTheme.NoActionBar">

</activity>

答案 2 :(得分:0)

使用addOnPageChangeListener

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

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    toolbar.setTitle(R.string.home);
                    break;
                case 1:
                    toolbar.setTitle(R.string.activities);
                    break;
                case 2:
                    toolbar.setTitle(R.string.people);
                    break;
                case 3:
                    toolbar.setTitle(R.string.posts);
                    break;
                case 4:
                    toolbar.setTitle(R.string.media);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {}
    });

答案 3 :(得分:0)

试试这个

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            switch (tab.getPosition()) {

                case 0:
                    viewPager.setCurrentItem(0);
                    toolbar.setTitle("Title_0");
                    break;

                case 1:
                    viewPager.setCurrentItem(1);
                    toolbar.setTitle(R.string.title_1);
                    break;

                case 2:
                    viewPager.setCurrentItem(2);
                    toolbar.setTitle(R.string.title_2);
                    break;

                case 3:
                    viewPager.setCurrentItem(3);
                    toolbar.setTitle(R.string.title_3);
                    break;

            }

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

答案 4 :(得分:0)

尝试一下。在tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

之后添加此代码
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            if (getSupportActionBar() != null){
                getSupportActionBar().setTitle(tab.getText());
            }
        }
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {}
        @Override
        public void onTabReselected(TabLayout.Tab tab) {}
    });