How to Make Dynamic Tablayout in Android Studio

Asked

Viewed 212 times

1

I created a tablayout in my application, I need that by clicking on one of the menu options, it automatically takes the 0 position.

inserir a descrição da imagem aqui

Where the house would be would be an icon to return and the others would be the categories of this particular item. When you click again on the first item that would be the return icon it returns a previous menu.

inserir a descrição da imagem aqui

I tried to remove all tables by selecting one of the tabs and adding new ones, but from the error in the application. It follows how the tablayout structure is:

xml

<com.conformidade.petrobras.apco.helper.CustomTabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabLayout"
        android:background="@color/colorPrimary"
        app:tabMode="scrollable"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</com.conformidade.petrobras.apco.helper.CustomTabLayout>

code

 //Configura Menu
        tabLayout = findViewById(R.id.tabLayout);

tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_home_white_24dp));
        tabLayout.addTab(tabLayout.newTab().setText("Menu 1"));
        tabLayout.addTab(tabLayout.newTab().setText("Menu 2"));
        tabLayout.addTab(tabLayout.newTab().setText("Menu 3"));
        tabLayout.addTab(tabLayout.newTab().setText("Menu 4"));


        tabLayout.setOnTabSelectedListener(new CustomTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(CustomTabLayout.Tab tab) {
                mostrarSubCategorias(tab);
            }

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

            }

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

            }
        });

Method to reload tables (Here it error in tabLayout.removeTabAt(j) )

private void mostrarSubCategorias(CustomTabLayout.Tab tab) {
        int totalTabs = tabLayout.getTabCount();
        int i = tab.getPosition();
        for (int j = 0; j < totalTabs; j++) {
            if (j != i) {
                tabLayout.removeTabAt(j);
            }
        }
        tab.setIcon(R.drawable.ic_keyboard_return_white_24dp).setText("");
    }

Does anyone have any idea how else to do that? Because the categories will be dynamic coming from a database where an administrator can delete or add new categories with sub-categories at any time.

  • 1

    Ola @Sanoli, do not change the question to indicate which/how the problem has been solved. If you have found a different response from the community’s proposals, you can answer your own question. I returned the version of your question to the revision 5. Just take a look at our [Tour]. = D

1 answer

1


The question was not answered, but I was able to figure out what the problem was, in the loop he was searching the tabs from the first index. It turns out that after removing the former the index of the other tabs changed and gave error exactly after half the total number. I just made the method search from the last position and go removing. Updated on the question for anyone who wants to do something similar.

Method to reload tables:

How was:

Here it error in tabLayout.removeTabAt(j)

private void mostrarSubCategorias(CustomTabLayout.Tab tab) {
        int totalTabs = tabLayout.getTabCount();
        int i = tab.getPosition();
        for (int j = 0; j < totalTabs; j++) {
            if (j != i) {
                tabLayout.removeTabAt(j);
            }
        }
        tab.setIcon(R.drawable.ic_keyboard_return_white_24dp).setText("");
    }

Updated method:

private void mostrarSubCategorias(CustomTabLayout.Tab tab) {
    tab.setIcon(R.drawable.ic_keyboard_return_white_24dp).setText("");
    int totalTabs = tabLayout.getTabCount();
    int i = tab.getPosition();
    for (int j = totalTabs - 1; j >= 0; j--) {
        if (j != i) {
            tabLayout.removeTabAt(j);
        }
    }
    //Aqui pode adicionar as novas tabelas
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.