How to detect a click on an already selected tab in TabLayout
Asked Answered
G

2

5

My question has been answered several times when it's a TabActivity with tabWidgets. But I couldn't find anything about the relativly new TabLayout view.

It's quite simple, like facebook, I want a tap on an already selected tab to get my list to scroll up til its beggining. but I can't figure out where to put my listener.

I tried on the TabLayout itself, mTabLayout.getChildAt() and on TabLayout.getTabAt().getCustomView().


EDIT : Correction : As CommonsWare mentionned in the comment of its answer, I had to rewrite the behaviour of "onTabSelected".

mTabLayout = (TabLayout) findViewById(R.id.tabs);
mTabLayout.setupWithViewPager(mViewPager);

for (int i = 0; i < mTabLayout.getTabCount(); i++) {
    mTabLayout.getTabAt(i).setIcon(tabIcons[i]);
}

mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        if (tab.getPosition() == PAGE_FEED) {
                ((PagerNewsFeedFragment) mViewPagerAdapter.getRegisteredFragment(PAGE_FEED)).scrollToTop();
            }
    }
});

Thanks !

Greatest answered 9/3, 2016 at 15:35 Comment(0)
I
16

Based on spending ~10 seconds reading the documentation... call setOnTabSelectedListener() on the TabLayout. Pass in an OnTabSelectedListener implementation. Your event callback method is onTabReselected().

Imprimatur answered 9/3, 2016 at 15:39 Comment(7)
yeah.. maybe that's why there is no stackoverflow about it. Doesn't work for me thought, I can't trigger the event. I'll post my codeGreatest
It was due to ordering of my code lines. I don't know why but it doesn't work if setupWithViewPager is called after setting the listenerGreatest
@RenaudFavier: The one concrete subclass of OnTabSelectedListener is ViewPagerOnTabSelectedListener. Calling setupWithViewPager() probably replaces your own listener. You'll need to work out how best to handle the event you want and also have ViewPager integration.Imprimatur
I'm sorry to ask but I don't understand your last sentence : "You'll need to work out how best to handle the event you want and also have ViewPager integration"Greatest
@RenaudFavier: I would assume that ViewPagerOnTabSelectedListener is doing something useful, such as switching the ViewPager current item based on tab selections. Presumably, you still want that behavior. You also want to know when a tab is reselected. You'll have to figure out how to blend those two. It may be a matter of extending ViewPagerOnTabSelectedListener yourself and tying it to your ViewPager. I haven't tried this, so I don't have an exact recipe for you.Imprimatur
well, you're right, the tab initial behaviour is lost. But what did they do beside switching tabs ? I just have to rewrite this part i guessGreatest
Try using addOnTabSelectedListener instead to prevent conflicts.Stagecoach
L
2

To not lose the default behaviour use it like this

TabLayout tabLayout = findById(this, R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        // Scroll to top or whatever
    }
});
Lear answered 3/9, 2016 at 0:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.