简体   繁体   English

从另一个标签刷新标签

[英]Refresh tab from another tab

I have an Activity that contains a ViewPager with four tabs . 我有一个Activity,其中包含一个带有四个选项卡的ViewPager。 each tab has a fragment. 每个选项卡都有一个片段。

When I click something in tab 4, I want tab 3 to refresh(or access a non-static method in tab 3 ) ..I've searched but all I found had this in it: 当我单击选项卡4中的内容时,我希望选项卡3刷新(或访问选项卡3中的非静态方法)..我进行了搜索,但发现的所有内容都包含在其中:

FragmentB fragment = (FragmentB)getFragmentManager().findFragmentByTag("FragmentB");

and I can't use it since I never set a tag on the fragment. 而且我无法使用它,因为我从未在片段上设置标签。

I also could call a function from fragment in another fragment but it has to be static , and I don't want that since everything inside it must be static too and that would mess things up for me.. 我也可以从另一个片段中的片段调用一个函数,但它必须是静态的,并且我不希望那样,因为其中的所有内容也必须是静态的,这会给我带来麻烦。

is there a solution to this ? 有针对这个的解决方法吗 ?

First of all i think it's not a good practice to update a view which user cannot see on screen at the moment. 首先,我认为更新用户当前无法在屏幕上看到的视图不是一个好习惯。 It's much better to update your data when user navigates to screen. 当用户导航到屏幕时,最好更新您的数据。 By the way it not our first concern now. 顺便说一句,这不是我们现在最关心的。

I'm not sure this is still a valid way to find Fragments in a ViewPager byTag but you can find them like below: 我不确定这仍然是在ViewPager byTag中查找Fragments的有效方法,但是您可以像下面这样找到它们:

This method generates default Tag for a Fragment in ViewPager . 此方法为ViewPagerFragment生成默认Tag

@NonNull
public static String generateViewPagerFragmentTag(int viewPagerId, int position) {
    final StringBuilder tagBuilder = new StringBuilder();
    tagBuilder.append("android:switcher:");
    tagBuilder.append(viewPagerId);
    tagBuilder.append(":");
    tagBuilder.append(position);
    return tagBuilder.toString();
} 

Than you can use this tag to find your Fragment with findFragmentByTag method. 比起您可以使用此标记通过findFragmentByTag方法查找您的Fragment

Also as mentioned in comments you can use an EventBus library to achieve what you want to do but be careful of Fragment Lifecycle states in ViewPager because the Fragment you want to communicate can be in onPause state an your changes canned be received. 另外,如注释中所述,您可以使用EventBus库来实现所需的操作,但是请注意ViewPagerFragment Lifecycle状态,因为要传达的Fragment可能处于onPause状态,可以接收您的更改。 This depends on in which lifecycle method you subscribe and unsubscribe to bus. 这取决于您订阅和取消订阅总线的生命周期方法。 (If you are using OttoBus you can get no subscribers found for this event exception.) You can do same pattern with interfaces . (如果使用的是OttoBus,则无法找到此事件异常的订阅者。)可以使用interfaces进行相同的interfaces You can create an interface and implement in your Fragments . 您可以创建一个接口并在Fragments实现。

For an other solution, you can directly access our fragments in your ViewPager. 对于其他解决方案,您可以直接在ViewPager中访问我们的片段。 Here's my answer for another question it. 这是对另一个问题的回答

Finally, as i mentioned at the beginning i think you should implement a solution which updates your data when user switched to specific tab of ViewPager . 最后,正如我在开始时提到的,我认为您应该实现一个解决方案,当用户切换到ViewPager特定选项卡时更新您的数据。 You can keep your data changes in a mem-cache and listen tab changes and update your view when user exactly navigated to that screen and ready to see data changes. 您可以将数据更改保存在内存缓存中,并在用户完全导航到该屏幕并准备查看数据更改时侦听选项卡更改并更新视图。 You can check Repository pattern for a more complex solution. 您可以检查Repository pattern以获得更复杂的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM