Various tabMode for TabLayout

I am using ViewPager and TabLayout. If the tabs can be placed on the displayMode tab, they should be:

app:tabMode="fixed"

else

app:tabMode="scrollable"

How can i do this?

+4
source share
4 answers

I won’t get your question, but I can help you if the number of tabs is static or fixed (you know the number of tabs), then app:tabMode="fixed"  if the number of tabs is dynamic (data comes from the feed), thenapp:tabMode="scrollable"

+1
source

In xml layout I declared

fixed

and in the java fragment class I did the following:

    if (tabLayout == null) {
        tabLayout = (TabLayout) view.findViewById(R.id.tabs);

        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);

        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        double wi = (double) width / (double) metrics.xdpi;
        double hi = (double) height / (double) metrics.ydpi;
        double x = Math.pow(wi, 2);
        double y = Math.pow(hi, 2);
        double screenInches = Math.sqrt(x + y);

        if (screenInches < 4) {
            tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        }
    }
+1
source

, Android.

: tabMode = "fixed" app: tabMode = "scrollable"

  • tabMode = "fixed" , .

  • app: tabMode = "scrollable". . , . ViewPager.

0

, (, , ..) , , / , , - / .

, , , . , , scrollable fixed .

res/layout/tabs.xml TablLayout :

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tabs"
    app:tabMode="scrollable"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

res/layout-land/tabs.xml TabLayout, :

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

, , :

<include layout="@layout/tabs" />

, , .

0
source

All Articles