从未选中的选项卡中调用片段的方法

huangapple go评论106阅读模式
英文:

Call method from Fragment in unselected tab

问题

  1. tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
  2. @Override
  3. public void onTabSelected(TabLayout.Tab tab) {
  4. String tag = "android:switcher:" + viewPager.getId() + ":" + viewPager.getCurrentItem();
  5. Fragment f = getSupportFragmentManager().findFragmentByTag(tag);
  6. ((MyFragment) f).someMethod();
  7. }
  8. @Override
  9. public void onTabUnselected(TabLayout.Tab tab) {
  10. String tag = "android:switcher:" + viewPager.getId() + ":" + tab.getPosition();
  11. Fragment f = getSupportFragmentManager().findFragmentByTag(tag);
  12. if (f != null) {
  13. // Call the method on the unselected fragment
  14. ((MyFragment) f).someUnselectedMethod();
  15. }
  16. }
  17. @Override
  18. public void onTabReselected(TabLayout.Tab tab) {
  19. }
  20. });
英文:

I have an application with tabs. I use ViewPager and TabLayout. In each tab I have a Fragment. When the tab is unselected I want to call a method from the fragment in that tab.

If I want to call method on selected fragment i will do:

  1. tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
  2. @Override
  3. public void onTabSelected(TabLayout.Tab tab) {
  4. String tag = "android:switcher:" + viewPager.getId() + ":" + viewPager.getCurrentItem();
  5. Fragment f = getSupportFragmentManager().findFragmentByTag(tag);
  6. ((MyFragment) f).someMethod();
  7. }
  8. @Override
  9. public void onTabUnselected(TabLayout.Tab tab) {
  10. }
  11. @Override
  12. public void onTabReselected(TabLayout.Tab tab) {
  13. }
  14. });

based on the https://stackoverflow.com/questions/39555081/how-to-know-fragment-id-for-the-fragments-provided-by-the-tabbed-activity-temp

But the problem is, that I don't know how to do it on unselected tab.

Can you help me with this?

答案1

得分: 1

你可以尝试在onTabUnselected方法中运行你的代码,但要注意这个片段可能会在下一步被销毁。

  1. @Override
  2. public void onTabUnselected(TabLayout.Tab tab) {
  3. // 标记选项卡位置
  4. String tag = "android:switcher:" + viewPager.getId() + ":" + tab.getPosition();
  5. Fragment f = getSupportFragmentManager().findFragmentByTag(tag);
  6. if (f instanceof MyFragment) {
  7. ((MyFragment) f).someMethodWhenUnselecting();
  8. }
  9. }

还要记住,ViewPager仅在当前页和左右各保留一个Fragment在内存中。所以,如果你从位置5移动到位置4,那么位置3及以下的内容就不存在了。

英文:

you can try run your code in onTabUnselected method, but be aware that this fragment may be destroyed in a next step.

  1. @Override
  2. public void onTabUnselected(TabLayout.Tab tab) {
  3. // tag for tab position
  4. String tag = "android:switcher:" + viewPager.getId() + ":" + tab.getPosition();
  5. Fragment f = getSupportFragmentManager().findFragmentByTag(tag);
  6. if(f instanceof MyFragment) {
  7. ((MyFragment) f).someMethodWhenUnselecting();
  8. }
  9. }

also remember that ViewPager keeps in memory only current and one Fragment to the left and one to the right of current. so if you moving from e.g. 5 position to 4 then position 3 and below aren't existing

pozdrowienia

huangapple
  • 本文由 发表于 2020年8月31日 20:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63670833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定