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

The following examples show how to use com.google.android.material.tabs.TabLayout#addOnTabSelectedListener() . 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: SampleFragments.java    From EasyPhotos with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample_fragments);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setOffscreenPageLimit(2);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

}
 
Example 2
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 3
Source File: MultiFragmentsActivity.java    From AndroidAnimationExercise with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_multi_fragments);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
    tabLayout.setupWithViewPager(mViewPager);

}
 
Example 4
Source File: ViewsActivity.java    From AndroidAnimationExercise with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_views);


    mFragments.add(new CircleMenuFragment());

    tabs.add("环形菜单");


    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setupWithViewPager(mViewPager);
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

}
 
Example 5
Source File: TransportModePanel.java    From msdkui-android with Apache License 2.0 5 votes vote down vote up
/**
 * Init content.
 */
private void init(final Context context) {
    setOrientation(VERTICAL);
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    LayoutInflater.from(context).inflate(R.layout.transport_mode_panel, this);
    mViewPager = (ViewPager) findViewById(R.id.view_pager);
    mViewPager.setVisibility(GONE);
    mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    mTabLayout.addOnTabSelectedListener(this);
    mDataSetObserver = new DataSetChangeObserver();
    setAdapter(new SimpleTransportModePanelAdapter(getContext()));
}
 
Example 6
Source File: ItemPagerAdapter.java    From materialistic with Apache License 2.0 5 votes vote down vote up
public void bind(ViewPager viewPager, TabLayout tabLayout,
                 FloatingActionButton navigationFab, FloatingActionButton genericFab) {
    viewPager.setPageMargin(viewPager.getResources().getDimensionPixelOffset(R.dimen.divider));
    viewPager.setPageMarginDrawable(R.color.blackT12);
    viewPager.setOffscreenPageLimit(2);
    viewPager.setAdapter(this);
    tabLayout.setupWithViewPager(viewPager);
    mTabListener = new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            super.onTabSelected(tab);
            toggleFabs(viewPager.getCurrentItem() == 0, navigationFab, genericFab);
            Fragment fragment = getItem(viewPager.getCurrentItem());
            if (fragment != null) {
                ((LazyLoadFragment) fragment).loadNow();
            }
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            Fragment fragment = getItem(viewPager.getCurrentItem());
            if (fragment != null) {
                ((Scrollable) fragment).scrollToTop();
            }
        }
    };
    tabLayout.addOnTabSelectedListener(mTabListener);
    viewPager.setCurrentItem(mDefaultItem);
    toggleFabs(mDefaultItem == 0, navigationFab, genericFab);

}
 
Example 7
Source File: TabLayoutHelper.java    From android-tablayouthelper with Apache License 2.0 5 votes vote down vote up
protected void setupWithViewPager(@NonNull TabLayout tabLayout, @NonNull ViewPager viewPager) {
    final PagerAdapter adapter = viewPager.getAdapter();
    if (adapter == null) {
        throw new IllegalArgumentException("ViewPager does not have a PagerAdapter set");
    }

    setTabsFromPagerAdapter(tabLayout, adapter, viewPager.getCurrentItem());

    viewPager.getAdapter().registerDataSetObserver(mInternalDataSetObserver);

    viewPager.addOnPageChangeListener(mInternalTabLayoutOnPageChangeListener);
    viewPager.addOnAdapterChangeListener(mInternalOnAdapterChangeListener);

    tabLayout.addOnTabSelectedListener(mInternalOnTabSelectedListener);
}
 
Example 8
Source File: MainActivity.java    From TowerCollector with Mozilla Public License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(MyApplication.getCurrentAppTheme());
    super.onCreate(savedInstanceState);
    Timber.d("onCreate(): Creating activity");
    // set fixed screen orientation
    if (!ApkUtils.isRunningOnBuggyOreoSetRequestedOrientation(this))
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.main);
    activityView = findViewById(R.id.main_root);
    //setup toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
    toolbar.setPopupTheme(MyApplication.getCurrentPopupTheme());
    setSupportActionBar(toolbar);
    // setup tabbed layout
    MainActivityPagerAdapter pageAdapter = new MainActivityPagerAdapter(getSupportFragmentManager(), getApplication());
    viewPager = (ViewPager) findViewById(R.id.main_pager);
    viewPager.setAdapter(pageAdapter);
    tabLayout = (TabLayout) findViewById(R.id.main_tab_layout);
    tabLayout.setupWithViewPager(viewPager);

    tabLayout.addOnTabSelectedListener(this);

    backgroundTaskHelper = new BackgroundTaskHelper(this);

    displayNotCompatibleDialog();

    // show latest developer's messages
    displayDevelopersMessages();

    // show introduction
    displayIntroduction();

    processOnStartIntent(getIntent());

    // check for availability of new version
    checkForNewVersionAvailability();

    registerReceiver(airplaneModeBroadcastReceiver, new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        batterySaverBroadcastReceiver = new BatterySaverBroadcastReceiver();
        registerReceiver(batterySaverBroadcastReceiver, new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
    }
}
 
Example 9
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 10
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 11
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()]
            )
    );
}
 
Example 12
Source File: MobiComKitPeopleActivity.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (!MobiComUserPreference.getInstance(this).isLoggedIn()) {
        finish();
    }
    setContentView(R.layout.people_activity);
    String jsonString = FileUtils.loadSettingsJsonFile(getApplicationContext());
    if (!TextUtils.isEmpty(jsonString)) {
        alCustomizationSettings = (AlCustomizationSettings) GsonUtils.getObjectFromJson(jsonString, AlCustomizationSettings.class);
    } else {
        alCustomizationSettings = new AlCustomizationSettings();
    }

    onContactsInteractionListener = this;
    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);
    // Set up the action bar.
    actionBar = getSupportActionBar();
    if (!TextUtils.isEmpty(alCustomizationSettings.getThemeColorPrimary()) && !TextUtils.isEmpty(alCustomizationSettings.getThemeColorPrimaryDark())) {
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(alCustomizationSettings.getThemeColorPrimary())));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.parseColor(alCustomizationSettings.getThemeColorPrimaryDark()));
        }
    }
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowTitleEnabled(true);


    intentExtra = getIntent();
    action = intentExtra.getAction();
    type = intentExtra.getType();

    if (getIntent().getExtras() != null) {
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            actionBar.setTitle(getString(R.string.send_message_to));
        } else {
            actionBar.setTitle(getString(R.string.search_title));
            userIdArray = getIntent().getStringArrayExtra(USER_ID_ARRAY);
        }
    } else {
        actionBar.setTitle(getString(R.string.search_title));
    }
    appContactFragment = new AppContactFragment(userIdArray);
    appContactFragment.setAlCustomizationSettings(alCustomizationSettings);
    channelFragment = new ChannelFragment();
    setSearchListFragment(appContactFragment);
    if (alCustomizationSettings.isGroupsSectionTabHidden() || ApplozicSetting.getInstance(this).isGroupsSectionTabHidden()) {
        addFragment(this, appContactFragment, "AppContactFragment");
    } else {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setVisibility(View.VISIBLE);
        setupViewPager(viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setVisibility(View.VISIBLE);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.addOnTabSelectedListener(this);
    }

    // This flag notes that the Activity is doing a search, and so the result will be
    // search results rather than all contacts. This prevents the Activity and Fragment
    // from trying to a search on search results.
    isSearchResultView = true;
}