ViewPager addOnPageChangeListener not working on same tabClick

前端 未结 2 455
一整个雨季
一整个雨季 2021-01-06 06:56

I have a ViewPager with addOnPageChangeListener. ViewPager has 3 tabviews (tab1, tab2, tab3). When a user clicks tab2, it loads some d

相关标签:
2条回答
  • 2021-01-06 07:28

    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(...);
    
    0 讨论(0)
  • 2021-01-06 07:29

    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.

    0 讨论(0)
提交回复
热议问题