简体   繁体   English

ViewPager 中同一 Fragment 的多个实例有问题吗?

[英]Issues with multiple instances of same Fragment in ViewPager?

Context: SubFragment1上下文: SubFragment1

Desired result: In onRefresh I want x to == z, in the example z is 1.预期结果:在 onRefresh 中,我希望 x 为 == z,在示例中 z 为 1。

Problematic Result:有问题的结果:

我的结果

Solution Removing the multiple instances of SubFragment1 FragmentPagerAdapter解决方案删除 SubFragment1 FragmentPagerAdapter的多个实例

@Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new SubFragment1(...);
            case 1:
                return new SubFragment1(...);
            case 2:
                return new SubFragment1(...);
            default:
                return null;
        }
    }

Question: Any idea how using multiple instances of one fragment within a viewPager, would break break the fragment's logic or standard lifecycle behaviour?问题:知道如何在 viewPager 中使用一个片段的多个实例会破坏片段的逻辑或标准生命周期行为吗? Nothing was static, variables were private.没有什么是静态的,变量是私有的。

Yeah, buddy, I was in such mess once.是的,伙计,我曾经陷入如此混乱的境地。

So what I did was, I used Five instances of same SubFragment and boy I was in trouble.所以我所做的是,我使用了五个相同的SubFragment实例,而我遇到了麻烦。 Basically I had Five different screens with similar functionality(responsibility) but almost the same layout(UI design).基本上我有五个不同的屏幕,它们具有相似的功能(职责)但几乎相同的布局(UI 设计)。 As a result, I decided to use the same SubFragment just to promote the principle of Code Reusability .因此,我决定使用相同的SubFragment只是为了宣传Code SubFragment的原则。

Initially, the requirements were small and everything was smooth, but later as changes(requirements) kept on piling I had nightmares managing them.最初,需求很小,一切都很顺利,但后来随着更改(需求)不断堆积,我做了噩梦来管理它们。

Let me give you an example:让我给你举个例子:

So let's say there is a method checkLocationAndNotify() which will check if a location is available and will show a Dialog if no location was found.因此,假设有一个方法checkLocationAndNotify()将检查位置是否可用,如果没有找到位置,将显示一个Dialog

Now I wanted this method to be called only when my third instance of SubFragment would open on ViewPager (User Swipes and reach third Child on ViewPager ).现在,我希望仅当我的第三个SubFragment实例在SubFragment打开时才调用此方法(用户ViewPager并到达ViewPager上的第三个 Child )。

So I placed this method call in the onViewCreated() lifecycle method of SubFragment .所以我把这个方法调用的onViewCreated()的生命周期方法SubFragment Boom this was the first of my troubles. Boom 这是我的第一个麻烦。 As soon as the user opened any of the ViewPager child, irrespective of the instance of SubFragment this method was called and kept showing LocationDialog every time.只要用户打开任何ViewPager子级,无论SubFragment的实例如何, SubFragment此方法并每次都保持显示LocationDialog

To resolve this, I had to put a check with respect to childPosition inside ViewPager .要解决这个问题,我不得不把支票相对于childPositionViewPager Also, I had to repeat this wherever I wanted some logic to work only on a certain instance of SubFragment .此外,我必须在任何我想要一些逻辑只在SubFragment的某个实例上工作的地方重复这SubFragment

Golden Advice:黄金建议:

If dealing with Fragments (even with slightly different responsibility) that too inside ViewPager please use different Fragments and promote the Single Responsibility principle over Code Reusability .如果在ViewPager内部处理Fragments (即使职责略有不同),请使用不同的Fragments促进单一职责原则而不是代码可重用性

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

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