-1
There are different ways to work with Tabs. This is how I normally use in my projects.
I released a simple Android Studio project on github: Slidingtablayoutsample.
I use two classes (SlidingTabLayout.java
and SlidingTabStrip.java
) available on the google github at this address: google/iosched/widget.
Add these classes to the project (as in the example).
Done this, now it is quite simple. It will take 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 published I created three Fragments. In the sequence I demonstrate one 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>
To finalise, it should include SlidingTabLayout
within the layout of AppBarLayout
. And ViewPager
within the CoordinatorLayout
. In 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 I made available on github to your computer and run the tests.
This here? How to create tabs in an android app ?
– Bacco