Reputation: 2744
I have created a popup DialogFragment with a ViewPager containing 3 tabs. I am able to swipe between these tabs, although I also want to be able to click on the tab headers to change between these tabs. Does anybody know if this is possible?
I have read that this is doable with the action bar layout, however since it is a popup dialog this isn't possible for me.
Here is my code:
The ViewPager Adapter:
public class TabAdapter extends FragmentPagerAdapter {
public TabAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Tab1();
case 1:
return new Tab2();
case 2:
return new Tab3();
default:
return null;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "First Tab";
case 1:
return "Second Tab";
case 2:
return "Third Tab";
}
return null;
}
}
The DialogFragment:
public class TabFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow() .setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tablayoutfragment, container);
TabAdapter tabAdapter = new TabAdapter(getChildFragmentManager());
ViewPager viewPager = (ViewPager) view.findViewById(R.id.pager);
viewPager.setAdapter(tabAdapter);
return view;
}
}
Each of the tabs just contain a text view currently, nothing exciting there. Please let me know if you see anything that I have missed
Upvotes: 2
Views: 2072
Reputation: 9808
Yes, you can do this! You don't need to use the ActionBar to have these tabs. Use a PagerTitleStrip instead of the ActionBar tabs and implement it exactly as described in the rest of the linked article.
Edit:
Alternatively you could use ViewPagerIndicator, developed by Jake Wharton. The usage would be the same as the linked article too.
Upvotes: 3