Select Tab in Tablayout (android)

0

Next, I'm making an app and using tablayout on multiple screens. But in all cases this problem happens. When I use the swipe to select the fragment on the side, the fragment changes, but the selected tab remains the first one. 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: (notice that selecting the second tab via swipe, the first one is still selected)

    
asked by anonymous 22.01.2017 / 15:46

1 answer

0

You just have to do it this way below to work correctly.

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.

    
22.01.2017 / 17:35