更改页面标题和片段 Android

Change the title of the page along with the fragment Android

我认为问题在于:

        {
           ..
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

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

        }

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

        }
    });
     ..
     this.setTitle("Home");

    }

和:

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            Home home = new Home();
            return Home;
        case 1:
            Mail mail = new Mail();
            return mail;
        default:
            return null;
    }
}

我现在想显示新的 Fragment setTitle

来代替页面标题
@Override
public Fragment getItem(int position) {

switch (position) {
    case 0:
        Home home = new Home();
        this.setTitle("change");  
        return Home;
    case 1:
        Mail mail = new Mail();
        this.setTitle("change");       
        return mail;
    default:
        return null;
}

我知道那是不正确的,但我不知道正确的语法是什么我整天都在努力解决,但一无所获。 我只想更改标题。

final ArrayList<String> arrayList = new ArrayList<>();
    arrayList.add("Menu1");
    arrayList.add("Menu2");
    arrayList.add("Menu3");
    arrayList.add("Menu4");
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            getSupportActionBar().setTitle(arrayList.get(position));
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int pos) {
            // TODO Auto-generated method stub

        }
    });