Tabs on Android - How to create [duplicate]

-1

How can I develop WhatsApp style tabs.

As the image below?

I use Android 5+

    
asked by anonymous 19.08.2016 / 04:34

1 answer

1

There are different ways of working with Tabs. This is the way I normally use it in my projects.

Made available a simple Android Studio project in github: SlidingTabLayoutSample .

I use two classes ( SlidingTabLayout.java and SlidingTabStrip.java ) that are available in google github at this address: google / iosched / widget .

Add these classes to the project (as in the example).

This is now quite simple. You will need an adapter:

TabsAdapter.java :

public class TabsAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragments = new ArrayList<>();
    private final List<String> mFragmentTitles = new ArrayList<>();

    public TabsAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        mFragments.add(fragment);
        mFragmentTitles.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitles.get(position);
    }
}

In the example I posted, I created three fragments. The following shows a them (layout and class).

FragmentUm.java :

public class FragmentUm extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_um, container, false);
    }
}

The layout fragment_um.xml :

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="24sp"
        android:textColor="@android:color/tertiary_text_dark"
        android:text="PRIMEIRA ABA"/>

</FrameLayout>

Finally, you should include SlidingTabLayout within the AppBarLayout layout. E ViewPager within CoordinatorLayout . This way:

The layout activity_main.xml :

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

        <br.com.krothx.slidingtablayoutsample.extras.SlidingTabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            app:tabMode="scrollable"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>

And the activity MainActivity.java :

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Inicializa ViewPager e carrega as tabs
        initViewPager();
    }

    private void initViewPager() {
        // Instancia o ViewPager a partir do resource adicionado no layout activity_main.xml
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

        if (viewPager != null) {
            setupViewPager(viewPager);
        }

        // Da mesma forma o SlidingTabLayout, também incluso no layout activity_main.xml
        SlidingTabLayout tabLayout = (SlidingTabLayout) findViewById(R.id.tabs);
        //noinspection ConstantConditions
        tabLayout.setSelectedIndicatorColors(Color.WHITE);
        tabLayout.setTextColorResId(R.color.tabs_text_color);

        // Adicionando um callback para disparar eventos ao realizar ações com as abas.
        tabLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener()  {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                Log.i(TAG, "Tab #" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        tabLayout.setViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        // Instancia o adapter para adicionar cada fragment que será construído em cada aba.
        TabsAdapter adapter = new TabsAdapter(getSupportFragmentManager());
        adapter.addFragment(new FragmentUm(), "PRIMEIRO");
        adapter.addFragment(new FragmentDois(), "SEGUNDO");
        adapter.addFragment(new FragmentTres(), "TERCEIRO");
        viewPager.setAdapter(adapter);
    }
}

If you want, you can clone the project that I made available in github for your computer and run the tests.

    
19.08.2016 / 12:50