ภาพรวม
ตัวอย่างนี้สาธิตการสร้าง Tab Layout พร้อม ViewPager โดยใช้ Material Design Support Library แสดง Fragment ต่างๆ ในแต่ละ Tab
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio → File → New Project และกรอกข้อมูลที่จำเป็น
ขั้นตอนที่ 2: เพิ่ม Dependency (build.gradle Module:app)
dependencies {
implementation 'com.android.support:design:28.0.0'
// AndroidX ใหม่:
// implementation 'com.google.android.material:material:1.11.0'
// implementation 'androidx.viewpager2:viewpager2:1.0.0'
// implementation 'androidx.fragment:fragment:1.6.2'
}
ขั้นตอนที่ 3: เลย์เอาต์หลัก (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1db995" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tabLayout" />
</RelativeLayout>
ขั้นตอนที่ 4: Activity หลัก (src/MainActivity.java)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tabLayout);
viewPager = findViewById(R.id.viewPager);
// เพิ่ม Tab
tabLayout.addTab(tabLayout.newTab().setText("Football"));
tabLayout.addTab(tabLayout.newTab().setText("Cricket"));
tabLayout.addTab(tabLayout.newTab().setText("NBA"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
// Adapter
MyAdapter adapter = new MyAdapter(this, getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
// Sync ViewPager กับ TabLayout
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override public void onTabUnselected(TabLayout.Tab tab) {}
@Override public void onTabReselected(TabLayout.Tab tab) {}
});
}
}
ขั้นตอนที่ 5: Adapter (MyAdapter.java)
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentManager;
class MyAdapter extends FragmentPagerAdapter {
Context context;
int totalTabs;
public MyAdapter(Context c, FragmentManager fm, int totalTabs) {
super(fm);
context = c;
this.totalTabs = totalTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: return new Football();
case 1: return new Cricket();
case 2: return new NBA();
default: return null;
}
}
@Override
public int getCount() {
return totalTabs;
}
}
ขั้นตอนที่ 6: สร้าง Fragments
Football.java / fragment_football.xml
// Football.java
public class Football extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_football, container, false);
}
}
// fragment_football.xml
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:text="Football Fragment"
android:textSize="16sp"
android:textStyle="bold" />
</FrameLayout>
Cricket.java / fragment_cricket.xml
// Cricket.java
public class Cricket extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_cricket, container, false);
}
}
// fragment_cricket.xml
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:text="Cricket Fragment"
android:textSize="16sp"
android:textStyle="bold" />
</FrameLayout>
NBA.java / fragment_nba.xml
// NBA.java
public class NBA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_nba, container, false);
}
}
// fragment_nba.xml
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:text="NBA Fragment"
android:textSize="16sp"
android:textStyle="bold" />
</FrameLayout>
ขั้นตอนที่ 7: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
การทดสอบ
รันแอป แท็บ 3 แท็บ สลับได้โดยสไลด์หรือกด Tab

อัปเดตสำหรับ AndroidX + ViewPager2 (แนะนำปัจจุบัน)
Dependencies
dependencies {
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.viewpager2:viewpager2:1.0.0'
implementation 'androidx.fragment:fragment:1.6.2'
}
Layout (ใช้ ViewPager2)
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tabLayout" />
Adapter ใหม่ (FragmentStateAdapter)
public class MyAdapter extends FragmentStateAdapter {
public MyAdapter(@NonNull FragmentActivity fa) {
super(fa);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position) {
case 0: return new Football();
case 1: return new Cricket();
case 2: return new NBA();
default: return new Football();
}
}
@Override
public int getItemCount() {
return 3;
}
}
เชื่อม TabLayout กับ ViewPager2
new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
tab.setText(new String[]{"Football", "Cricket", "NBA"}[position]);
}).attach();
✅ ข้อดีของ ViewPager2: รองรับ RTL, Vertical orientation, Modifier notifyDataSetChanged(), DiffUtil, FragmentStateAdapter (มี Lifecycle จัดการ Fragment เอง)
TabLayout Customization
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed" <!-- fixed | scrollable -->
app:tabGravity="fill" <!-- fill | center -->
app:tabIndicatorColor="@color/white"
app:tabIndicatorHeight="3dp"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="@color/gray"
app:tabTextAppearance="@style/CustomTabText" />