ViewPager addOnPageChangeListener无法在同一个选项卡Click上工作



我有一个ViewPageraddOnPageChangeListenerViewPager有3个选项卡视图(tab1、tab2、tab3)。当用户单击tab2时,它会加载一些数据(基本上是RecyclerView)。此时,如果用户再次单击tab2,我需要重新加载数据,但在这种情况下不会触发addOnPageChangeListener

我的代码:

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

原始答案:

安卓团队已经改变了TabLayout和ViewPager之间的一些对话方式。阅读文档。但事情并没有得到很好的解释。我在代码中包含了很多注释。我希望这能有所帮助。

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

如果您有任何其他问题,请查看此问题。

第1版:2015年12月

不是这个问题的解决方案,但总的来说很有帮助

tabLayout.setupWithViewPager(viewPager);

这样,您就不必担心在选择选项卡时自己设置片段。在这种情况下不再需要CCD_ 6。这是在引擎盖下处理的。当您不需要对选项卡进行太多控制时(比如在选择/点击相同选项卡时重新加载片段),这很有用。

更新:2018年5月

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

上述两个函数都已弃用。初始化视图寻呼机+表格布局,如下所示:

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

听起来您的onPageSelected回调没有被调用,因为页面实际上没有更改。我认为,如果您编写TabLayout.ViewPagerOnTabSelectedListener的自定义子类,即使所选页面索引没有更改,也可以使用onTabReselected回调来触发刷新。

您应该能够在调用setupWithViewPager之后使用TabLayout::setOnTabSelectedListener安装自定义OnTabSelectedListener

相关内容

  • 没有找到相关文章

最新更新