Usually, an icon in TabLayout is placed like this:

 tabLayout.getTabAt(0).setIcon(R.drawable.ic_menu_tabone); 

... but I want to do it right in the fragment constructor, which we will load when selecting the tab:

 public static TabOne getInstance(Context context){ Bundle args = new Bundle(); TabOneFragment fragment = new TabOneFragment(); fragment.setArguments(args); fragment.setContext(context); fragment.setTitle(context.getString(R.string.tabOneTitle)); // работает fragment.setIcon(R.drawable.ic_menu_tabone); // нет эффекта, но приложение работает return fragment; } 

What I don't understand is: how does TabLayout determine that fragment.setTitle(context.getString(R.string.tabOneTitle)); - it was the command to set the header on the tab, and why in the same way (i.e. fragment.setIcon(R.drawable.ic_menu_inbox); ) does not want to put an icon?

The fragment is inherited from the abstract class:

 public class AbstractFragment extends Fragment{ private String title; private int iconID; protected Context context; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public void setIcon(int iconID){ this.iconID = iconID; } } 

MainActivity:

 private void initTabs(){ viewPager = (ViewPager) findViewById(R.id.viewPager); TabsFragmentAdapter adapter = new TabsFragmentAdapter(this, getSupportFragmentManager()); viewPager.setAdapter(adapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout); tabLayout.setupWithViewPager(viewPager); // Хочу по-другому // tabLayout.getTabAt(0).setIcon(R.drawable.ic_menu_tabone); } 

Adapter:

 public class TabsFragmentAdapter extends FragmentPagerAdapter { private Map<Integer, AbstractTabFragment> tabs; private Context context; public TabsFragmentAdapter(Context context, FragmentManager fm) { super(fm); this.context = context; initTabsMap(context); } public CharSequence getPageTitle(int position){ return tabs.get(position).getTitle(); } public void getTabIcon(int position){ //return tabs.get(position).getIcon(); tabs.get(position).setIcon(R.drawable.ic_menu_inbox); } @Override public Fragment getItem(int position) { return tabs.get(position); } @Override public int getCount() { return tabs.size(); } private void initTabsMap(Context context){ tabs = new HashMap<>(); tabs.put(0, TabOneFragment.getInstance(context)); tabs.put(1, TabTwoFragment.getInstance(context)); tabs.put(2, TabThreeFragment.getInstance(context)); tabs.put(3, TabFourFragment.getInstance(context)); } } 

Just in case, I’ll say that I don’t need the headers and I will immediately remove fragment.setTitle() as soon as the icons appear.

  • Show the installation code of the icon in tabLayout - pavel163
  • In the sense of the adapter? Added by. I suppose this is not what you wanted to see, but I don’t understand how to install the icon inside the constructor correctly, because there is no other installation code. - Bokov Gleb
  • Where do you call this method tabLayout.getTabAt(0).setIcon(R.drawable.ic_menu_tabone); ? - pavel163
  • At the time of writing the question - due to lack of understanding was not anywhere. Then I tried it like this: public void getTabIcon(int position){ tabs.get(position).setIcon(R.drawable.ic_menu_inbox); } public void getTabIcon(int position){ tabs.get(position).setIcon(R.drawable.ic_menu_inbox); } in the adapter. There is no effect. - Bokov Gleb
  • Please post the entire adapter - pavel163

0