Reputation: 13258
I have a TabListener in Android defined in this way:
public static class TabListener<T extends Fragment>
implements ActionBar.TabListener { ... }
An I have this source code:
Tab myTab = myActionBar.
newTab().
setText("Home").
setTabListener(new TabListener<MyFragment>(
this,
"Home",
MyFragment.class
));
...
Now I want to put this into a method:
addTab("Home", ???);
private void addTab(String text, ???) {
Tab myTab = myActionBar.
newTab().
setText(text).
setTabListener(new TabListener<???>(
this,
text,
???.class
));
...
}
What I have to fill in instead of ???
?
Upvotes: 1
Views: 4112
Reputation: 606
Your tab listener needs the type parameter to be a subclass of Fragment
public static class TabListener<T extends Fragment>
Therefore, you need to make sure it is the case in your code
addTab("Home", ???);
private <T extends Fragment> void addTab(String text, Class<T> clazz) {
Tab myTab = myActionBar.
newTab().
setText(text).
setTabListener(new TabListener<T>(
this,
text,
clazz
));
...
}
Upvotes: 5
Reputation: 45433
addTab("Home", MyFragment.class);
private void addTab(String text, Class<? extends Fragment> clazz) {
Tab myTab = myActionBar.
newTab().
setText(text).
setTabListener(new TabListener<>(
this,
text,
clazz
));
...
}
Upvotes: 3
Reputation: 4786
Something like this is probably what you're looking for:
private <T> void addTab(String text, Class<T> clazz) {
Tab myTab = myActionBar.
newTab().
setText(text).
setTabListener(new TabListener<T>(
this,
text,
clazz
));
...
}
Upvotes: 2