Home > front end >  How to add a TabItem by activity?
How to add a TabItem by activity?

Time:04-13

I need the TabLayout to have an "x" number of tabs, which can vary according to the user's data that is already in the database

My xml: enter code here<com.google.android.material.tabs.TabLayout android:id="@ id/select_bar" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:tabContentStart="32dp" app:tabMode="scrollable">

        <com.google.android.material.tabs.TabItem
            android:id="@ id/posto1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Posto Shell" />

        <com.google.android.material.tabs.TabItem
            android:id="@id/posto2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Posto Guanabara" />

My code:

select_bar.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
            when (tab?.position) {
                0 -> {
                    recycler_view_abastecimentos.adapter =
                        ListaAbastecimentoAdapter(applicationContext, lista)
                }
                1 -> {
                    recycler_view_abastecimentos.adapter =
                        ListaAbastecimentoAdapter(applicationContext, lista)
                }
            }
        }

CodePudding user response:

You can do so by:

 TabLayout tabLayout = ...;
 tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
 tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
 tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));

See the documentation here: https://developer.android.com/reference/com/google/android/material/tabs/TabLayout

CodePudding user response:

 PagerAdapter pagerAdapter = new PagerAdapter(fragmentManager,
        getActivity());

mViewPager.setOffscreenPageLimit(dataCategory.size());

mViewPager.setAdapter(pagerAdapter);

mTabLayout.setupWithViewPager(mViewPager);



class PagerAdapter extends FragmentPagerAdapter {
        Context context;

        PagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }

        @Override
        public int getCount() {
            return dataCategory.size();
        }

        @Override
        public Fragment getItem(int position) {
            return SubFragment.newInstance(dataCategory.get(position),position, gridType);
         }

        @Override
        public CharSequence getPageTitle(int position) {
            // Generate title based on item position
            // return tabTitles[position];
            return dataCategory.get(position).getCat_Name();
        }

        
    }

CodePudding user response:

You can achieve this by making a list. From your list, you can dynamically create your tabs.

Example:

final TabLayout tabLayout = findViewById(R.id.tab_layout);
final ArrayList<Hobby> hobbyList = getHobbyListFromDb();
if (hobbyList.size() > 0) {
   for (int i = 0; i < hobbyList.size(); i  ) {
      tabLayout.addTab(tabLayout.newTab().setText(hobbyList.get(i).getName()));
   }
}
  • Related