TabMode = "scrollable" does not work

I am using TabLayout from the Android Design Support Library. I use the material design theme. Below is the code

activity_scrollable_Tab.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 <android.support.design.widget.AppBarLayout

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"/>

    </android.support.design.widget.AppBarLayout>

   <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

scrollableTabActivity.java

public class ScrollableTabsActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new OneFragment(), "ONE");
        adapter.addFrag(new TwoFragment(), "TWO");
        .....
        adapter.addFrag(new TenFragment(), "TEN");
        viewPager.setAdapter(adapter);
    }

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

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

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

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

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

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

Tab scrolling works in Lollipop. But the same code does not work on Kitkat and Jelly bean devices. On Kitkat and Jelly bean, I have two problems,

  • When I look at the tab, the Tab scrolls along with the page navigation in the Jellybean and kitkat devices, but only the Lollipop tab scrolls and this is the working file. For example: if 1,2,3, .10 are a tab, and if I scroll the tab, it scrolls, and for each scroll the layout moves. If I scroll, Tab goes to the second tab, and then to the third, etc.

  • Jellybean Kitkat. , ( ).

- , , . APK, .

android, , .

+4
6

, ? setTabMode TabLayaut tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

, , , Android.

, , , - ?

+3

- , , .

xmlns:app="http://schemas.android.com/apk/res-auto" android.support.design.widget.CoordinatorLayout.

,

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

xmlns:app="http://schemas.android.com/apk/res-auto" XML TabLayout Tab bean, Kitkat lollipop.

+2

, xml TabLayout.

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:tabMode="scrollable" />
0

FragmentPagerAdapter , ?

adapter.notifyDataSetChanged();

viewPager.setAdapter(adapter);

setupViewPager?

0

Just a warm reminder to a beginner like me, he needs a lot of elements (larger than the size of the screen width) to make it scrollable, so don't expect it to scroll if you are testing with only two elements, and this is not the same thing something like "scroll to switch tab" that you can search for, tabMode = "scrollable" only scroll without switch.

0
source

Source: https://habr.com/ru/post/1610107/


All Articles