Java 将TabBar的tabItem放在中间,单击“下一个选项卡”后,它将开始向左移动?

Java 将TabBar的tabItem放在中间,单击“下一个选项卡”后,它将开始向左移动?,java,android,tabs,Java,Android,Tabs,当我们单击任何选项卡项时,选项卡项应该从移动设备的中间开始。Tabitem应该在中间。当它到达末尾时,Tabitem应该在中间。就像从最后开始的第一个位置。 Java代码 tabLayout=findViewById(R.id.tabs); viewPager=findViewById(R.id.frameLayout); for (int k = 0; k <10; k++) { tabLayout.addTab(tabLayout.new

当我们单击任何选项卡项时,选项卡项应该从移动设备的中间开始。Tabitem应该在中间。当它到达末尾时,Tabitem应该在中间。就像从最后开始的第一个位置。


Java代码

    tabLayout=findViewById(R.id.tabs);
    viewPager=findViewById(R.id.frameLayout);

    for (int k = 0; k <10; k++) {
        tabLayout.addTab(tabLayout.newTab().setText("" + k));
    }

    PlansPagerAdapter adapter = new PlansPagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(1);
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));//Bonus Code : If your tab layout has more than 2 tabs then tab will scroll other wise they will           take whole width of the screen
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout=findviewbyd(R.id.tabs);
viewPager=findViewById(R.id.frameLayout);

对于(int k=0;k显示您的XML布局添加的XML和Java代码,我正在使用@NiceumangtabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);请在删除此行后重试。@Niceumang我需要我的tabitem看起来像这样image@MujeebRahamanT您可以使用自定义库获得以上结果,您是否灵活使用它?
    tabLayout=findViewById(R.id.tabs);
    viewPager=findViewById(R.id.frameLayout);

    for (int k = 0; k <10; k++) {
        tabLayout.addTab(tabLayout.newTab().setText("" + k));
    }

    PlansPagerAdapter adapter = new PlansPagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(1);
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));//Bonus Code : If your tab layout has more than 2 tabs then tab will scroll other wise they will           take whole width of the screen
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);