ArclightOne
ArclightOne

Reputation: 39

ActionBar Title with Fragments/ViewPager - Title shown for wrong Fragment

I've looked around for a couple days trying to figure this issue out and I can't seem to narrow it down to my specific problem.

I have an app that makes use of 4 Fragments, the usual FragmentPagerAdapter, TabLayout, etc. I'm using the normal ActionBar and want to update the title of the ActionBar to reflect which of the 4 Fragments I'm currently on.

I've already implemented someone's solution to change the title, but it didn't really work:

((MainActivity) getActivity()).setActionBarTitle("Locations");

With the above code, for some strange reason I can't understand, the title is always for the next Fragment over from the current one I'm on! So when I'm on the "Food" Fragment, the Title says "Photos", which is the title of next Fragment after it. I've tried putting this code in my onCreateView(), onActivityCreated(), and onResume() for each Fragment, but the result is the same - the title is not for the correct Fragment.

I'll my current code so that any issues can be spotted hopefully...

Note: I do not want to change the title of each Tab in the TabLayout, as I have icons set for those, I only want to change the ActionBar's title in relation to the current displayed Fragment. :)

FragmentPagerAdapter:

public class CategoryAdapter extends FragmentPagerAdapter {

    private Context mContext;

    public CategoryAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            return new Locations();
        } else if (position == 1) {
            return new Food();
        } else if (position == 2) {
            return new Photos();
        } else {
            return new History();
        }
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return null;
    }
}

MainActivity class:

public class MainActivity extends AppCompatActivity {

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


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


        CategoryAdapter adapter = new CategoryAdapter(this, getSupportFragmentManager());

        viewPager.setAdapter(adapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

        int[] imageResId = {
                R.drawable.ic_place_white_48dp,
                R.drawable.ic_restaurant_white_48dp,
                R.drawable.ic_wallpaper_white_48dp,
                R.drawable.ic_account_balance_white_48dp};

        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            tabLayout.getTabAt(i).setIcon(imageResId[i]);
        }
    }

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

Any ideas? Thanks so much for your help!

Upvotes: 0

Views: 746

Answers (1)

KeLiuyue
KeLiuyue

Reputation: 8237

You can try this .

1.use setupWithViewPager method

2.you must use setIcon method after setupWithViewPager method

3.set default ActionBar title

4.set ActionBar title when tab is selected

5.remove ((MainActivity) getActivity()).setActionBarTitle("Locations"); in fragment

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

    CategoryAdapter adapter = new CategoryAdapter(this, getSupportFragmentManager());

    viewPager.setAdapter(adapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    int[] imageResId = {
            R.drawable.ic_place_white_48dp,
            R.drawable.ic_restaurant_white_48dp,
            R.drawable.ic_wallpaper_white_48dp,
            R.drawable.ic_account_balance_white_48dp};
    // title
    final String[] mTitleNames = {"location", "food", "photos", "history"};
    // setupWithViewPager
    tabLayout.setupWithViewPager(viewPager);
    // you must setIcon after setupWithViewPager method
    for (int i = 0; i < imageResId.length; i++) {
        tabLayout.getTabAt(i).setIcon(imageResId[i]);
    }
    // set default actionbar title
    setActionBarTitle(mTitleNames[0]);
    // set actionbar title when tab is selected
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int pos = tab.getPosition();
            setActionBarTitle(mTitleNames[pos]);
        }

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

        }

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

        }
    });
}

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

Upvotes: 1

Related Questions