Help to migrate from Actionbar.Tabs to ViewPager because of API 21

1

As I understand it lightly, the hosting of tabs in the actionbar was obsolete in API 21. I still do not understand very well because (if someone explains it better I thank) but what I can do is adapt. I did a search and found some alternatives like Sliding Tabs from Google Play with ViewPager, although I have never used ViewPager because Actionbar.Tab served me very well.

But I have some problems and doubts in the implementation because this tool seems to be very good for static tabs. The case is that my app has very dynamic tabs. Because it is a chat app with every conversation being displayed by a fragment, new tabs pop up and are removed at all times.

I was not able to align ViewPager strategy very well with this scenario. I noticed that the way the adapter identifies the selected tab is very different from the actionbar tablistener, and I also did not find methods that added or removed tabs on the adapter or SlidingTabs. I thought about storing the fragments in an Arraylist to be used by the adapter but I do not know if the adapter would identify when I removed or added a fragment from there. Another issue that dynamizes the tabs is that users' nicknames related to the conversation are displayed on them, and that nick can also change at any time. I'm not sure when the getPageTitle () method is called.

Thinking about these differences I decided to come here to ask for help because maybe there is even a better alternative to this case that I do not yet know or some good practice that leaves this scenario viable.

Any help is welcome. Thanks in advance.

    
asked by anonymous 11.04.2015 / 19:05

1 answer

2

Hello, first I'd like to tell you about my experience regarding Dynamic Fragments within ViewPager and I can tell you that is not a good idea. I have no idea how your app is but if it was good advice (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 list the conversations and another Fragment / Activity that shows the conversation. I also think about usability, for example: I have 10 Tabs / Conversations open and to be able to open the 9th I need to keep rolling the ViewPager until I get there O_o

But by clarifying how ViewPager works, you should think about the structure of it. 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 Tabs will be (title or any layout you want) and your ViewPager which is where all your Fragments will be added.

Next, in the Fragment / Activity you want to implement the ViewPager you should create an Adapter for it and set the 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 the ViewPager, in this case is the field PAGE_COUNT;

You can, of course, change the amount of Fragments in your Adapter so you have to create it by passing in as many Fragments as you want and a list of Fragments that you want to use. Or you can also create these methods within the Adapter, modify them, and inform the controller by the Adapter% method of%.

In this example I used the framework link

    
13.04.2015 / 20:12