Hello, first I would like to say about my experience regarding Dynamic Fragments within the Viewpager and I can tell you that is not a good idea. I have no idea what your app looks like but if advice were good (rsrs) I would tell you to think about how most messaging apps work (Hangouts, fb messenger, Whatsapp, Telegram, etc.), where there is a Fragment that lists the conversations and another Fragment/Activity that shows the conversation. I also think about usability, for example: I have 10 Tabs/ Open Conversations and in order to open the 9th I need to scroll the Viewpager until I get there O_o
But clarifying how Viewpager works, you should think about its structure. First xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_gravity="top"
android:textSize="16sp"
android:background="@color/color_primary"
app:pstsIndicatorColor="@color/color_primary_darker"
app:pstsTextAllCaps="false"
app:pstsIndicatorHeight="2dp"
app:pstsPaddingMiddle="false"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white"/>
</LinearLayout>
Very simple where you indicate where the Tabs will be (title or any layout you want) and your Viewpager which is where all your Fragments will be added.
Then, in the Fragment/Activity you want to implement Viewpager you must create an Adapter for it and set Viewpager in Slidingtab:
mPager = (ViewPager) v.findViewById(R.id.viewpager);
mPagerTab = (PagerSlidingTabStrip) v.findViewById(R.id.sliding_tabs);
mPager.setAdapter(new ViewPagerAdapter(getActivity().getSupportFragmentManager(), tabtitles, mDrawable));
mPagerTab.setViewPager(mPager);
And a simple example of a Fragmentpageradapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String titles[];
public ViewPagerAdapter(FragmentManager fm, String[] titles) {
super(fm);
titles = titles;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return SampleFragment.newInstance();
case 1:
return SampleFragment.newInstance();
case 2:
return SampleFragment.newInstance();
}
return null;
}
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public int getCount() {
return PAGE_COUNT;
}
}
What determines the amount of Fragments in Viewpager, in this case* is the PAGE_COUNT field;
It is possible yes you keep changing the amount of Fragments in your Adapter for this you need to create it by passing as parameter the amount of Fragments you want and a list with the Fragments you want to use. Or you can also create these methods within the Adapter, modify them and inform the controller by the method notifyDataSetChanged();
of the Adapter.
In this example I used the framework https://github.com/jpardogo/PagerSlidingTabStrip
So in my case this PAGE_COUNT could no longer be final, it will vary. And for me to better understand, if I want to add or remove a tab, I certainly have an Arraylist<Fragment> to store the Fragments and if there is any change, I call notifyDataSetChanged()?
– Informatheus
That’s right. It works the same way you can on a Listview with a Basedapter
– Rafael Neiva
Looking that way can even get simpler than with actionbar.tabs. That is, if it works.
– Informatheus
Rafael, how do I know the name of the selected tab?
– Informatheus
You can use the method
getCurrentItem()
from Viewpager that returns an integer.– Rafael Neiva
Could you tell me what is the difference between this Sliding tabs from jpardogo to that of astuetz and which I should use and pq? Thank you very much Rafael.
– Informatheus
Basically jpardogo is based on Material Design and astuetz on Holo Theme (standard theme of ICS, JB and KK)
– Rafael Neiva