android–ViewPager addOnPageChangeListener不能在同一个tabClick上工作

android–ViewPager addOnPageChangeListener不能在同一个tabClick上工作,第1张

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

我有一个带有addOnPagechangelistener的VIEwPager. VIEwPager有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);

解决方法:

原答案:

AndroID团队已经改变了一些关于TabLayout和VIEwPager如何相互通信的事情. Read the docs.但事情没有得到很好的解释.我在代码中包含了很多注释.我希望有所帮助.

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 populatedtabLayout.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 tabsvIEwPager.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    }});

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

编辑1:2015年12月

不是这个问题的解决方案,但总体上有帮助.

tabLayout.setupWithVIEwPager(vIEwPager);

这样,在选择选项卡时,您无需担心自己设置片段.在这种情况下不再需要tabLayout.setonTabSelectedListener(..).这是在under the hood处理的.当您不需要对选项卡进行过多控制时(例如,在选择/点击相同选项卡时重新加载片段),这非常有用.

更新:2018年5月

tabLayout.setTabsFromPagerAdapter(adapter);tabLayout.setonTabSelectedListener(...);

上述两个函数都已弃用.初始化vIEwpager tablayout,如下所示:

vIEwPager.setAdapter(adapter);tabLayout.setupWithVIEwPager(vIEwPager); // this will automatically bind tab clicks to vIEwpager fragmentsvIEwPager.addOnPagechangelistener(TabLayout.TabLayoutOnPagechangelistener(tabLayout))// do additional tab clicks here// no need to manually set vIEwpager item based on tab clicktabLayout.addOnTabSelectedListener(...);
总结

以上是内存溢出为你收集整理的android – ViewPager addOnPageChangeListener不能在同一个tabClick上工作全部内容,希望文章能够帮你解决android – ViewPager addOnPageChangeListener不能在同一个tabClick上工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115756.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存