简体   繁体   中英

Select third page as default in TabLayout + ViewPager2

I'm trying to make third tab as default, but below piece of code does not work:

public class MessagesFragment extends Fragment {

    private FragmentsAdapter fragmentsAdapter;
    private ViewPager2 viewPager;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_messages, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        fragmentsAdapter = new FragmentsAdapter(this);
        viewPager = view.findViewById(R.id.pager);
        viewPager.setAdapter(fragmentsAdapter);
        viewPager.setCurrentItem(2);

        TabLayout tabLayout = view.findViewById(R.id.tab_layout);
        TabLayoutMediator.TabConfigurationStrategy tabConfigurationStrategy = new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                switch (position) {
                    case FragmentsAdapter.MESSAGES_NEW:
                        tab.setText(R.string.navMessagesNew);
                        break;
                    case FragmentsAdapter.MESSAGES_READ:
                        tab.setText(R.string.navMessagesRead);
                        break;
                    case FragmentsAdapter.SETTINGS:
                        tab.setText(R.string.navSettings);
                        break;
                    default:
                        throw new IllegalArgumentException("There is no fragment for that position = " + position);
                }
            }
        };
        new TabLayoutMediator(tabLayout, viewPager, tabConfigurationStrategy).attach();
    }
}

I'm using TabLayout and ViewPager2. All the time 1st tab is default. Can anyone advise how to make a 3rd of my tab as default? Thanks

I had the same problem, it looks like there is a problem with viewPager2, if you set the tab as soon as you add attach the TabLayoutMediator (using setCurrentItem) it has a weird behavior, select the tab but doesn't update the page of the viewPager.

But if you do it later, in a button or after a couple of seconds calling the method viewPager.setCurrentItem(2) works fine, in may case since I have to know to which tab I have to go I asked to my viewModel to tell me with tab I have to display and when the observer is nofity the tab is changing correctly.

At least for me this works but I can see the first tab load (the deault tab 0) and then the tab change for the one I selected, since this is not viable for me, I change to use viewPager (instead of viewPager2) and set it to the tabLayout as usual, tab_layout.setupWithViewPager(view_pager) .

With the regular viewPager works fine.

Your code is correct:

  • viewPager.setCurrentItem(2);
  • viewPager.setCurrentItem(2, false);
  • tabLayout.getTabAt(2).select();
  • tabLayout.selectTab(tabLayout.getTabAt(2));

these are the same things

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM