package com.example.blackdumed.computer_learning_media_v2;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by blackdumed on 17/8/2560.
*/
public class Tab0Fragment extends Fragment {
View view;
ViewPager viewPager;
TabLayout tabLayout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.sample, container, false);
viewPager = (ViewPager)view.findViewById(R.id.viewpager);
viewPager.setAdapter(new sliderAdapter(getChildFragmentManager()));
tabLayout = (TabLayout)view.findViewById(R.id.sliding_tabs);
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return view;
}
private class sliderAdapter extends FragmentPagerAdapter{
final String tabs[] = {"1", "2", "3", "4"};
public sliderAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0: return new Tab1Fragment();
case 1: return new Tab2Fragment();
case 2: return new Tab3Fragment();
case 3: return new Tab4Fragment();
}
return null;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position){
return tabs[position];
}
}
}
รูปครับ :
https://drive.google.com/open?id=0B-Y076jWkOtDU1ZsYjBKX1AxTXM
Android Studio : กำหนด Icon ในแท็บแต่ละแท็บ ทำไงครับ
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by blackdumed on 17/8/2560.
*/
public class Tab0Fragment extends Fragment {
View view;
ViewPager viewPager;
TabLayout tabLayout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.sample, container, false);
viewPager = (ViewPager)view.findViewById(R.id.viewpager);
viewPager.setAdapter(new sliderAdapter(getChildFragmentManager()));
tabLayout = (TabLayout)view.findViewById(R.id.sliding_tabs);
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return view;
}
private class sliderAdapter extends FragmentPagerAdapter{
final String tabs[] = {"1", "2", "3", "4"};
public sliderAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0: return new Tab1Fragment();
case 1: return new Tab2Fragment();
case 2: return new Tab3Fragment();
case 3: return new Tab4Fragment();
}
return null;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position){
return tabs[position];
}
}
}
รูปครับ : https://drive.google.com/open?id=0B-Y076jWkOtDU1ZsYjBKX1AxTXM