Select Tab in Tablayout(android)

Asked

Viewed 677 times

0

Next, I’m making an app and use tablayout on several screens. Except that in all this problem happens. When I use Swipe to select the fragment on the side, the fragment changes, but the selected tab remains the first. The code:

public class RegrasTab extends AppCompatActivity implements TabLayout.OnTabSelectedListener{

private TabLayout tabLayout;

private ViewPager viewPager;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

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

    tabLayout.addTab(tabLayout.newTab().setText("Open 2.0"));
    tabLayout.addTab(tabLayout.newTab().setText("TK2"));

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

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

    PagerRegras adapter = new PagerRegras(getSupportFragmentManager(), tabLayout.getTabCount());

    viewPager.setAdapter(adapter);

   tabLayout.addOnTabSelectedListener(this);

}


@Override
public void onTabSelected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabReselected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());

}

Pagerregras.java:

public class PagerRegras extends FragmentStatePagerAdapter {

int tabCount;


public PagerRegras(FragmentManager fm, int tabCount) {
    super(fm);
    this.tabCount= tabCount;
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            TabOpen open = new TabOpen();
            return open;
        case 1:
            TabTK2 tk2 = new TabTK2();
            return tk2;
        default:
            return null;
    }
}

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

The images:(note that by selecting the second tab via Swipe, the first one remains selected)

Tab1 tab2

  • 1

    Gives error could not resolve getTabAt method.

  • Put your Pagerregras code here

  • edited the post with Pagerregras

  • Guy I edited and I diminished the size of your image, it gets really bad reading the question when the picture is giant. Ah, I answered your question by rearranging the code. It will work perfectly. Here it works for me. Abs.

1 answer

0


You just need to do it this way below to make it work properly.

public class RegrasTab extends AppCompatActivity {

    /* objetos referente as tabs*/
    private TabLayout tabLayout;
    private ViewPager viewPager;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regras_tab);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

         /* objetos referente as tabs*/
        viewPager = (ViewPager) findViewById(R.id.pager);
        setupViewPager(viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);

    }

    private void setupViewPager(ViewPager viewPager) {
        PagerRegras adapter = new PagerRegras(getSupportFragmentManager());
        adapter.addFrag(new TabOpen(), "OPEN 2.0");
        adapter.addFrag(new TabTK2(), "TK2");
        viewPager.setAdapter(adapter);
    }

    class PagerRegras extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        PagerRegras(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

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

        void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

Note: I simplified the code for better understanding.

  • Perfeito, só tive que mudar essa parte&#xA;viewPager = (ViewPager) findViewById(R.id.pager);&#xA; setupViewPager(viewPager);&#xA; tabLayout = (TabLayout) findViewById(R.id.tabLayout);&#xA; tabLayout.setupWithViewPager(viewPager);&#xA;E funcionou perfeitamente&#xA;Thank you so much for your help!

  • @Marceloawq really, I forgot to change the ids here to adapt to yours. But that’s it. Hugs.

  • @Marceloawq made the change to match. =)

Browser other questions tagged

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