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

The following examples show how to use com.google.android.material.tabs.TabLayout#addTab() . 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: MaleFemalePresenter.java    From Autocomplete with Apache License 2.0 6 votes vote down vote up
@NonNull
@Override
protected ViewGroup getView() {
    // RecyclerViewPresenter returns a RecyclerView. We inflate it in a bigger container.
    ViewGroup rv = super.getView();
    ViewGroup container = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.male_female_popup, null);
    // Add RecyclerView to our container
    ViewGroup rvContainer = container.findViewById(R.id.recycler_view_container);
    rvContainer.addView(rv, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    // Set up bar that reacts to clicks and syncs with 'females' boolean
    TabLayout tabLayout = container.findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Males"));
    tabLayout.addTab(tabLayout.newTab().setText("Females"));
    //noinspection ConstantConditions
    tabLayout.getTabAt(females ? 1 : 0).select();
    tabLayout.addOnTabSelectedListener(this);
    return container;
}
 
Example 2
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Adds tabs to {@link TabLayout} */
public static ViewAction addTabs(final String... tabs) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(TabLayout.class);
    }

    @Override
    public String getDescription() {
      return "TabLayout add tabs";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      TabLayout tabLayout = (TabLayout) view;
      for (int i = 0; i < tabs.length; i++) {
        tabLayout.addTab(tabLayout.newTab().setText(tabs[i]));
      }

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 3
Source File: HelpLazyLoadView.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText(R.string.comments));
    tabLayout.addTab(tabLayout.newTab().setText(R.string.article));
    Preferences.StoryViewMode defaultView = Preferences.getDefaultStoryView(getContext());
    int defaultTab;
    switch (defaultView) {
        case Comment:
        default:
            defaultTab = 0;
            break;
        case Article:
        case Readability:
            defaultTab = 1;
            break;
    }
    //noinspection ConstantConditions
    tabLayout.getTabAt(defaultTab).select();
}
 
Example 4
Source File: ViewPagerFragment.java    From AndroidNavigation with MIT License 5 votes vote down vote up
private void initView(View view) {

        toolbar = view.findViewById(R.id.toolbar);

        AppBarLayout appBarLayout = view.findViewById(R.id.appbar_layout);

        // important
        if (isStatusBarTranslucent()) {
            appendStatusBarPadding(appBarLayout);
        }

        TabLayout tabLayout = view.findViewById(R.id.tab_layout);
        ViewPager2 viewPager = view.findViewById(R.id.view_pager);

        tabLayout.addTab(tabLayout.newTab().setText("Android"));
        tabLayout.addTab(tabLayout.newTab().setText("Awesome"));
        tabLayout.addTab(tabLayout.newTab().setText("Navigation"));

        viewPager.setAdapter(new ViewPagerAdapter(this));

        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            String[] titles = new String[]{"Android", "Awesome", "Navigation"};
            tab.setText(titles[position]);
            viewPager.setCurrentItem(position);
        }).attach();

        viewPager.setCurrentItem(0);

    }
 
Example 5
Source File: SecondFragment.java    From ZhiHuIndex with Apache License 2.0 5 votes vote down vote up
private void InitView() {
    mTabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
    mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager);

    //初始化TabLayout的title
    mTabLayout.addTab(mTabLayout.newTab().setText("推荐"));
    mTabLayout.addTab(mTabLayout.newTab().setText("圆桌"));
    mTabLayout.addTab(mTabLayout.newTab().setText("热门"));
    mTabLayout.addTab(mTabLayout.newTab().setText("收藏"));

    List<String> titles = new ArrayList<>();
    titles.add("推荐");
    titles.add("圆桌");
    titles.add("热门");
    titles.add("收藏");

    //初始化ViewPager的数据集
    List<Fragment> fragments = new ArrayList<>();
    fragments.add(new SecondSubFragment());
    fragments.add(new SecondSubFragment());
    fragments.add(new SecondSubFragment());
    fragments.add(new SecondSubFragment());
    //创建ViewPager的adapter
    FragmentAdapter adapter = new FragmentAdapter(getChildFragmentManager(), fragments, titles);
    mViewPager.setAdapter(adapter);
    //千万别忘了,关联TabLayout与ViewPager
    //同时也要覆写PagerAdapter的getPageTitle方法,否则Tab没有title
    mTabLayout.setupWithViewPager(mViewPager);
    mTabLayout.setTabsFromPagerAdapter(adapter);

}
 
Example 6
Source File: TabLayoutHelper.java    From android-tablayouthelper with Apache License 2.0 5 votes vote down vote up
protected void setTabsFromPagerAdapter(@NonNull TabLayout tabLayout, @Nullable PagerAdapter adapter, int currentItem) {
    try {
        mDuringSetTabsFromPagerAdapter = true;

        int prevSelectedTab = tabLayout.getSelectedTabPosition();
        int prevScrollX = tabLayout.getScrollX();

        // remove all tabs
        tabLayout.removeAllTabs();

        // add tabs
        if (adapter != null) {
            int count = adapter.getCount();
            for (int i = 0; i < count; i++) {
                TabLayout.Tab tab = createNewTab(tabLayout, adapter, i);
                tabLayout.addTab(tab, false);
                updateTab(tab);
            }

            // select current tab
            currentItem = Math.min(currentItem, count - 1);
            if (currentItem >= 0) {
                tabLayout.getTabAt(currentItem).select();
            }
        }

        // adjust tab mode & gravity
        if (mAutoAdjustTabMode) {
            adjustTabMode(prevScrollX);
        } else {
            // restore scroll position if needed
            int curTabMode = tabLayout.getTabMode();
            if (curTabMode == TabLayout.MODE_SCROLLABLE) {
                tabLayout.scrollTo(prevScrollX, 0);
            }
        }
    } finally {
        mDuringSetTabsFromPagerAdapter = false;
    }
}