Among most of the TabLayout examples I’ve seen, all tabs had one class for all tabs. How to technically create TabLayout with tabs, each with its own separate class?
I also ask you to make a comment to the decision, because in all the lessons that I saw, there is a catastrophic lack of clarification, and in the documentation, I feel there are many unnecessary details that are completely incomprehensible how to apply in practice (and here is an example from the documentation, where the integration of TabLayout and ViewPager mockingly written 2 short paragraphs).
Base code:
MainActivity.java
public class MainActivity extends AppCompatActivity { private Toolbar toolbar; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { setTheme(R.style.AppDefault); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initToolbar(); initTabs(); } protected void initToolbar() { ... } protected void initTabs(){ viewPager = (ViewPager) findViewById(R.id.viewPager); TabsFragmentAdapter adapter = new TabsFragmentAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout); tabLayout.setupWithViewPager(viewPager); // Здесь показвыает предупреждение о возможном вылете // Method Invocation SetIcon can produce NullPointerException // tabLayout.getTabAt(0).setIcon(R.drawable.ic_menu_tabone); // tabLayout.getTabAt(1).setIcon(R.drawable.ic_menu_tabtwo); // ... } } Adapter
public class TabsFragmentAdapter extends FragmentPagerAdapter { private Map<Integer, Fragment> tabs; // Ключ - номер вкладки, значение - экземпляр фрагмента. // Конкретно в данной задаче в табах только иконки. public TabsFragmentAdapter(FragmentManager fm) { super(fm); } @Override public CharSequence getPageTitle(int position){ return fragmentTitles.get(position); } From now on, I don’t know what to do (the task is to display 4 tabs with icons without text). I used to do it as shown below and it worked, but in this question the getInstance() method, which was previously declared in the tab class, was rejected (I don’t argue that there were grounds for that).
public CharSequence getPageTitle(int position){ return tabs.get(position).getTitle(); } @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)); } } Tab example
public class TabOne extends Fragment { private static final int LAYOUT = R.layout.tab_one; public static OneTabFragment newInstance(int page){ Bundle args = new Bundle(); OneTabFragment fragment = new OneTabFragment(); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(LAYOUT, container, false); dBHandler = new dBHandler(getActivity()); BasicActivity activity = (BasicActivity)getActivity(); return view; } @Override public void onResume() { super.onResume(); // формирую отображение на основе данных из БД именно здесь, т. к. после первого прохода onCreate() // идёт onResume() } } By the way, I pointed out that in tabs there are images without captions only so that you do not create extra arrays in the answer if they are not needed, and of course pictures instead of captions are not the main thing in this matter.
FragmentOne,FragmentTwo,FragmentThreeand so on; each of which displays the contents of the fragment (as shown in the last block of code). Then according to the plan, when you click on the first tab, the contents ofFragmentOneshould be displayed, on the second -FragmentTwoand so on. - Bokov Gleb