Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager addOnPageChangeListener not working on same tabClick

I have a ViewPager with addOnPageChangeListener. ViewPager has 3 tabviews (tab1, tab2, tab3). When a user clicks tab2, it loads some data (Basically it a RecyclerView). At this moment, if user again clicks tab2, I need to reload the data but in this case addOnPageChangeListener is not triggered.

My Code:

customPagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(customPagerAdapter);

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {
    }

    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    public void onPageSelected(int position) {
        Log.i("TAG", "position: " + position);
        switch (position) {
            case 0:
                addingMarker(LocationData.find(LocationData.class, "Category = ? ", "1"));
                break;
            case 1:
                addingMarker(LocationData.find(LocationData.class, "Category = ? ", "2"));
                break;
            case 2:
                addingMarker(LocationData.find(LocationData.class, "Category = ? ", "3"));
                break;
        }
    }
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setupWithViewPager(viewPager);
like image 923
Anirban Avatar asked Dec 25 '22 11:12

Anirban


2 Answers

Original Answer:

The Android team has changed a few things about how TabLayout and ViewPager talk to each other. Read the docs. But things are not explained well. I've included a lot of comments in the code. I hope that helps.

final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
Adapter adapter = new Adapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);

// the tabs will get their titles from the adapter and get populated
tabLayout.setTabsFromPagerAdapter(adapter);

// this is done "so that the tab position is kept in sync"
// what it does is when you swipe the fragment in view pager, it updates the tabs
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

// without this listener the tabs would still get updated when fragments are swiped, but ....  (read the next comment)
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        Toast.makeText(MainActivity.this, "tabSelected:  " + tab.getText(), Toast.LENGTH_SHORT).show();
        // no where in the code it is defined what will happen when tab is tapped/selected by the user
        // this is why the following line is necessary
        // we need to manually set the correct fragment when a tab is selected/tapped
        // and this is the problem in your code
        viewPager.setCurrentItem(tab.getPosition());
    }

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

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        Toast.makeText(MainActivity.this, "tabReSelected:  " + tab.getText(), Toast.LENGTH_SHORT).show();

        // Reload your recyclerView here
    }
});

Check out this issue if you have got any other problems.

EDIT 1: December 2015

Not a solution to this question but helpful in general.

tabLayout.setupWithViewPager(viewPager);

This way you don't need to worry about setting the fragment yourself when a tab is selected. tabLayout.setOnTabSelectedListener(..) is no longer needed in this situation. That is handled under the hood. This is useful when you don't need too much control over your tabs(like reloading fragment when same tab is selected/tapped).

UPDATE: MAY 2018

tabLayout.setTabsFromPagerAdapter(adapter);
tabLayout.setOnTabSelectedListener(...);

Both of the above functions are deprecated. Initialize viewpager+tablayout as shown below:

viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager); // this will automatically bind tab clicks to viewpager fragments
viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout))

// do additional tab clicks here
// no need to manually set viewpager item based on tab click
tabLayout.addOnTabSelectedListener(...);
like image 123
denvercoder9 Avatar answered Dec 27 '22 00:12

denvercoder9


It sounds like your onPageSelected callback is not being called because the page isn't actually changing. I think if you write a custom subclass of TabLayout.ViewPagerOnTabSelectedListener, you can use the onTabReselected callback to trigger your refresh even those the selected page index hasn't changed.

You should be able to install your custom OnTabSelectedListener using TabLayout::setOnTabSelectedListener after you call setupWithViewPager.

like image 42
cwbowron Avatar answered Dec 27 '22 00:12

cwbowron