ViewPager addOnPageChangeListener not working on same tabClick

别说谁变了你拦得住时间么 提交于 2019-12-06 07:42:33

问题


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);

回答1:


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(...);



回答2:


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.



来源:https://stackoverflow.com/questions/34271137/viewpager-addonpagechangelistener-not-working-on-same-tabclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!