简体   繁体   English

getSupportLoaderManager()在不同的片段中使用相同的ID?

[英]getSupportLoaderManager() use same ids in different fragments?

I am making a viewPager in a FragmentActivity with multiple ListFragments. 我在FragmentActivity中使用多个ListFragments创建一个viewPager。 In order to populate the different listviews i get the LoaderManager and initiliaze in the loader. 为了填充不同的列表视图,我在加载器中获得LoaderManager和initiliaze。 I wanted to have a unique LoaderManager for each fragment, however, fragments don't have a getSupportLoaderManager() method so i need to call the parent fragment activity: 我希望每个片段都有一个唯一的LoaderManager,但是,片段没有getSupportLoaderManager()方法,所以我需要调用父片段活动:

getActivity().getSupportLoaderManager()

The problem with this approach that my Loaders id in different fragments conflicts with each others. 我的Loaders在不同片段中识别的这种方法的问题与彼此冲突。 It can get a little cumbersome when you already have multiple loaders id in one fragment. 当你已经在一个片段中拥有多个加载器id时,它会变得有点麻烦。 So is there a way to get an unique LoaderManager for each fragment, instead of calling the parent one? 那么有没有办法为每个片段获取一个唯一的LoaderManager,而不是调用父片段? I basically want to be able to use the same id on multiple fragments. 我基本上希望能够在多个片段上使用相同的id。

Thank you 谢谢

是的,您可以使用: android.support.v4.app.Fragment.getLoaderManager()为每个片段获取一个唯一的片段,这意味着您可以从零开始您的ID。

It would be too long a comment, but I continu our discussion within the comments of your question. 评论太长了,但我会在你的问题评论中继续我们的讨论。

You should take it the other way around : live with the fact that the IDs are local to an activity, not to a fragment. 你应该采取相反的方式:实际上ID是活动的本地,而不是片段。

Yes, there is poor coupling between fragments and activities. 是的,片段和活动之间的耦合很差。 Mostly call backs from fragments link the fragment to an activity (even when using an interface it's not that clean), and for passing arguments from an activity to a fragment : you must use a static factory method to both build the fragment and set its arguments via setArgs. 大多数来自片段的回调将片段链接到一个活动(即使使用它不是那么干净的接口),以及将参数从活动传递到片段:您必须使用静态工厂方法来构建片段并设置其参数通过setArgs。

And your question is all about such a coupling issue : loaders IDs in each fragment of an activity should not overlap over the IDs used by all other fragments. 而你的问题就是这样一个耦合问题:活动的每个片段中的加载器ID不应该与所有其他片段使用的ID重叠。 That's a coupling constraint as well, as we have a constraint for fragment that lies at the activity level. 这也是一个耦合约束,因为我们对位于活动级别的片段有一个约束。 You are right, that could maybe have been solved by Android at the fragment level by introducing loaders local to fragments. 你是对的,这可能是Android在片段级别通过向片段引入本地加载器来解决的。

Nevertheless, there might be elegant ways to solve that and preserve a relative de-coupling scheme. 然而,可能有优雅的方法来解决这个问题并保留相对解耦方案。 For instance, you could create a factory of IDs that would generate "good" IDs for loaders inside fragments and prevent overlapping (id++ would be perfect) : 例如,您可以创建一个ID工厂,为片段内的加载器生成“好”ID并防止重叠(id ++将是完美的):

public interface IdFactory {
   public int createId();
}

then inside each fragment, when you need a loader : 然后在每个片段内,当你需要一个加载器:

this.newLoaderId = idFactory.createId();

The factory could be shared by all fragments using one of the three strategies : 使用以下三种策略之一,所有碎片可以共享工厂:

  • a singleton. 单身人士 Each fragment will access the IdFactory via 每个片段都将通过访问IdFactory

    IdFactory idFactory = DefaultIdFactory.getInstance(); IdFactory idFactory = DefaultIdFactory.getInstance();

  • create a single instance of a class that implements IdFactory, make that instance of that class a data field in your application class an give it a getter. 创建一个实现IdFactory的类的单个实例,使该类的实例成为应用程序类中的数据字段,并为其提供一个getter。 Each fragment will be able to access it using 每个片段都可以使用它来访问它

    IdFactory idFactory = ((MyApplication)getActivity().getApplicationContext() ).getIdFactory(); IdFactory idFactory =((MyApplication)getActivity()。getApplicationContext())。getIdFactory();

  • create your factories at the activity level. 在活动级别创建工厂。 Either activities can implement the IdFactory interface or they can supply an inner class to do it. 任何一个活动都可以实现IdFactory接口,或者它们可以提供内部类来完成它。 Each fragment would then access the factory using : 然后每个片段将使用以下方式访问工厂:

    IdFactory idFactory = getActivity().getIdFactory(); IdFactory idFactory = getActivity()。getIdFactory(); //or IdFactory idFactory = (IdFactory)getActivity(); //或IdFactory idFactory =(IdFactory)getActivity();

The third option is the better as it will follow your exact need and will follow activities life cycles, allowing your factories to be garbage collected. 第三种选择是更好的,因为它将遵循您的确切需要,并将遵循活动生命周期,允许您的工厂被垃圾收集。

There are other options like using RoboGuice or Dagger or any other Dependency injection framework, but that is less standard. 还有其他选项,如使用RoboGuice或Dagger或任何其他依赖注入框架,但这不太标准。

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

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