2016-03-03 51 views
1

我有三个类使用我的自定义适配器,因此,我需要将它们传递为Fragments如何确定通过的“片段”对象的类型

我的工作,但我很难区分片段之间。

我现在有这样的事情作为我的构造函数:

public CustomAdapter(ArrayList<UserInfo> arrayList, UsersOne fragment){ 
    this.arrayList = arrayList; 
    this.fragment = fragment; 
} 

我的问题是,我有UsersTwo,并UsersThree这都是fragments无一不需要CustomAdapter。我如何修改我的构造函数以迎合所有三个构造函数,并且我可以调用它们的方法调用?

回答

3

使用instanceOf

做这个

if(fragment instanceOf UsersOne){ 
    // take action for UsersOne 
} 
else if(fragment instanceOf UsersTwo){ 
    // take action for UsersTwo 
} 
else if(fragment instanceOf UsersThree){ 
    // take action for UsersThreee 
} 
+0

这工作,感谢队友。我已经接受你的答案 –

相关问题