Java Code Examples for com.google.android.material.tabs.TabLayout#setTabGravity()

The following examples show how to use com.google.android.material.tabs.TabLayout#setTabGravity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TabLayoutHelper.java    From android-tablayouthelper with Apache License 2.0 5 votes vote down vote up
protected void adjustTabModeInternal(@NonNull TabLayout tabLayout, int prevScrollX) {
    int prevTabMode = tabLayout.getTabMode();

    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);

    int newTabMode = determineTabMode(tabLayout);

    cancelPendingUpdateScrollPosition();

    if (newTabMode == TabLayout.MODE_FIXED) {
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    } else {
        LinearLayout slidingTabStrip = (LinearLayout) tabLayout.getChildAt(0);
        slidingTabStrip.setGravity(Gravity.CENTER_HORIZONTAL);
        if (prevTabMode == TabLayout.MODE_SCROLLABLE) {
            // restore scroll position
            tabLayout.scrollTo(prevScrollX, 0);
        } else {
            // scroll to current selected tab
            mUpdateScrollPositionRunnable = new Runnable() {
                @Override
                public void run() {
                    mUpdateScrollPositionRunnable = null;
                    updateScrollPosition();
                }
            };
            mTabLayout.post(mUpdateScrollPositionRunnable);
        }
    }
}
 
Example 2
Source File: TabsControllableDemoFragment.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private void setAllTabLayoutGravity(int gravity) {
  for (TabLayout tabLayout : tabLayouts) {
    tabLayout.setTabGravity(gravity);
  }
}
 
Example 3
Source File: UserActivity.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void initPages() {
    photoPagerModel.readDataList(list ->
            adapters[ProfilePager.PAGE_PHOTO] = new PhotoAdapter(this, list).setItemEventCallback(
                    new PhotoItemEventHelper(
                            this, photoPagerModel, (context, photo) -> downloadPhoto(photo)
                    )
            )
    );

    likesPagerModel.readDataList(list ->
            adapters[ProfilePager.PAGE_LIKE] = new PhotoAdapter(this, list).setItemEventCallback(
                    new PhotoItemEventHelper(
                            this, likesPagerModel, (context, photo) -> downloadPhoto(photo)
                    )
            )
    );

    collectionsPagerModel.readDataList(list ->
            adapters[ProfilePager.PAGE_COLLECTION] = new CollectionAdapter(this, list).setItemEventCallback(
                    new CollectionItemEventHelper(this)
            )
    );

    List<View> pageList = new ArrayList<>(
            Arrays.asList(
                    new UserPhotosView(
                            this,
                            (PhotoAdapter) adapters[ProfilePager.PAGE_PHOTO],
                            ProfilePager.PAGE_PHOTO,
                            this
                    ), new UserPhotosView(
                            this,
                            (PhotoAdapter) adapters[ProfilePager.PAGE_LIKE],
                            ProfilePager.PAGE_LIKE,
                            this
                    ), new UserCollectionsView(
                            this,
                            (CollectionAdapter) adapters[ProfilePager.PAGE_COLLECTION],
                            ProfilePager.PAGE_COLLECTION,
                            this
                    )
            )
    );
    for (int i = 0; i < pageList.size(); i ++) {
        pagers[i] = (PagerView) pageList.get(i);
    }

    String[] userTabs = getResources().getStringArray(R.array.user_tabs);

    List<String> tabList = new ArrayList<>();
    Collections.addAll(tabList, userTabs);
    this.adapter = new PagerAdapter(viewPager, pageList, tabList);

    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(getCurrentPagerPosition(), false);
    viewPager.addOnPageChangeListener(this);

    TabLayout tabLayout = findViewById(R.id.activity_user_tabLayout);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.addOnTabSelectedListener(new TabLayoutDoubleClickBackToTopPresenter(UserActivity.this::backToTop));

    indicator.setViewPager(viewPager);
    indicator.setAlpha(0f);

    pagerManageModel.getPagerPosition().observe(this, position -> {
        for (int i = ProfilePager.PAGE_PHOTO; i < pageCount(); i ++) {
            pagers[i].setSelected(i == position);
        }

        if (pagerModels[getCurrentPagerPosition()].getListSize() == 0
                && pagerModels[getCurrentPagerPosition()].getListState() != ListResource.State.REFRESHING
                && pagerModels[getCurrentPagerPosition()].getListState() != ListResource.State.LOADING) {
            PagerViewManagePresenter.initRefresh(
                    (PagerViewModel<?>) pagerModels[getCurrentPagerPosition()],
                    adapters[getCurrentPagerPosition()]
            );
        }
    });

    for (int i = ProfilePager.PAGE_PHOTO; i < pageCount(); i ++) {
        int finalI = i;
        pagerModels[i].observeListResource(this, viewModel ->
                PagerViewManagePresenter.responsePagerListResourceChanged(
                        viewModel, pagers[finalI], adapters[finalI]
                )
        );
    }

    AnimUtils.translationYInitShow(viewPager, 400);
}
 
Example 4
Source File: MeActivity.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void initPages() {
    photoPagerModel.readDataList(list ->
            adapters[photosPage()] = new PhotoAdapter(this, list).setItemEventCallback(
                    new PhotoItemEventHelper(
                            this, photoPagerModel, (context, photo) -> downloadPhoto(photo)
                    )
            )
    );

    likesPagerModel.readDataList(list ->
            adapters[likesPage()] = new PhotoAdapter(this, list).setItemEventCallback(
                    new PhotoItemEventHelper(
                            this, likesPagerModel, (context, photo) -> downloadPhoto(photo)
                    )
            )
    );

    collectionsPagerModel.readDataList(list ->
            adapters[collectionsPage()] = new CollectionAdapter(this, list).setItemEventCallback(
                    new CollectionItemEventHelper(this)
            )
    );

    List<View> pageList = new ArrayList<>(
            Arrays.asList(
                    new MePhotosView(
                            this,
                            (PhotoAdapter) adapters[photosPage()],
                            photosPage(),
                            this
                    ), new MePhotosView(
                            this,
                            (PhotoAdapter) adapters[likesPage()],
                            likesPage(),
                            this
                    ), new MeCollectionsView(
                            this,
                            (CollectionAdapter) adapters[collectionsPage()],
                            collectionsPage(),
                            this
                    )
            )
    );
    for (int i = 0; i < pageList.size(); i ++) {
        pagers[i] = (PagerView) pageList.get(i);
    }

    String[] userTabs = getResources().getStringArray(R.array.user_tabs);

    List<String> tabList = new ArrayList<>();
    Collections.addAll(tabList, userTabs);
    this.adapter = new PagerAdapter(viewPager, pageList, tabList);

    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(getCurrentPagerPosition(), false);
    viewPager.addOnPageChangeListener(this);

    TabLayout tabLayout = findViewById(R.id.activity_me_tabLayout);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.addOnTabSelectedListener(new TabLayoutDoubleClickBackToTopPresenter(MeActivity.this::backToTop));

    indicator.setViewPager(viewPager);
    indicator.setAlpha(0f);

    pagerManageViewModel.getPagerPosition().observe(this, position -> {
        for (int i = photosPage(); i < pageCount(); i ++) {
            pagers[i].setSelected(i == position);
        }

        if (pagerModels[getCurrentPagerPosition()].getListSize() == 0
                && pagerModels[getCurrentPagerPosition()].getListState() != ListResource.State.REFRESHING
                && pagerModels[getCurrentPagerPosition()].getListState() != ListResource.State.LOADING) {
            PagerViewManagePresenter.initRefresh(
                    pagerModels[getCurrentPagerPosition()],
                    adapters[getCurrentPagerPosition()]
            );
        }
    });

    for (int i = photosPage(); i < pageCount(); i ++) {
        int finalI = i;
        pagerModels[i].observeListResource(this, viewModel ->
                PagerViewManagePresenter.responsePagerListResourceChanged(
                        viewModel, pagers[finalI], adapters[finalI]
                )
        );
    }

    AnimUtils.translationYInitShow(viewPager, 400);
}
 
Example 5
Source File: MyFollowActivity.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void initPages() {
    pagerModels[followerPage()].readDataList(list ->
            adapters[followerPage()] = new MyFollowAdapter(this, list).setItemEventCallback(
                    new MyFollowItemEventHelper(this)
            )
    );

    pagerModels[followingPage()].readDataList(list ->
            adapters[followingPage()] = new MyFollowAdapter(this, list).setItemEventCallback(
                    new MyFollowItemEventHelper(this)
            )
    );

    List<View> pageList = new ArrayList<>(
            Arrays.asList(
                    new MyFollowUserView(
                            this,
                            adapters[followerPage()],
                            followerPage(),
                            this
                    ), new MyFollowUserView(
                            this,
                            adapters[followingPage()],
                            followingPage(),
                            this
                    )
            )
    );
    for (int i = 0; i < pageList.size(); i ++) {
        pagers[i] = (PagerView) pageList.get(i);
    }

    String[] myFollowTabs = getResources().getStringArray(R.array.my_follow_tabs);

    List<String> tabList = new ArrayList<>();
    Collections.addAll(tabList, myFollowTabs);
    PagerAdapter adapter = new PagerAdapter(viewPager, pageList, tabList);
    if (AuthManager.getInstance().getUser() != null) {
        adapter.titleList.set(
                followerPage(),
                AuthManager.getInstance().getUser().followers_count + " " + myFollowTabs[followerPage()]
        );
        adapter.titleList.set(
                followingPage(),
                AuthManager.getInstance().getUser().following_count + " " + myFollowTabs[followingPage()]
        );
        adapter.notifyDataSetChanged();
    }

    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(this);
    viewPager.setCurrentItem(getCurrentPagerPosition(), false);

    TabLayout tabLayout = findViewById(R.id.activity_my_follow_tabLayout);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.addOnTabSelectedListener(new TabLayoutDoubleClickBackToTopPresenter(MyFollowActivity.this::backToTop));

    pagerManageModel.getPagerPosition().observe(this, position -> {
        for (int i = followerPage(); i < pageCount(); i ++) {
            pagers[i].setSelected(i == position);
        }
        if (pagerModels[position].getListSize() == 0
                && pagerModels[position].getListState() != ListResource.State.REFRESHING
                && pagerModels[position].getListState() != ListResource.State.LOADING) {
            PagerViewManagePresenter.initRefresh(pagerModels[position], adapters[position]);
        }
    });

    pagerModels[followerPage()].observeListResource(this, viewModel ->
            PagerViewManagePresenter.responsePagerListResourceChanged(
                    viewModel,
                    pagers[followerPage()],
                    adapters[followerPage()]
            )
    );
    pagerModels[followingPage()].observeListResource(this, viewModel ->
            PagerViewManagePresenter.responsePagerListResourceChanged(
                    viewModel,
                    pagers[followingPage()],
                    adapters[followingPage()]
            )
    );
}