http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/
https://guides.codepath.com/android/google-play-style-tabs-using-tablayout
Buat tampilan parent (activity_main.xml)
Tampilan Tab 2 (tab_fragment_2.xml)
Tab 1 (TabFragment1.java)
Tab 1 (TabFragment1.java)
Tab 2 (TabFragment2.java)
Tab 2 (TabFragment2.java)
Parent (MainActivity.java) ....(1)
dapat juga di implementasikan ke fragment
Parent (MainActivity.java) ....(2)
Parent (MainActivity.java) ....(3)
kelas adaptor (PagerAdaptor.java)
untuk melakukan custom dapat dilihat lebih jauh di ->
https://guides.codepath.com/android/google-play-style-tabs-using-tablayout
https://guides.codepath.com/android/google-play-style-tabs-using-tablayout
Buat tampilan parent (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<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>
Tampilan Tab 1 (tab_fragment_1.xml)<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="Tab 1"android:textAppearance="?android:attr/textAppearanceLarge"/></RelativeLayout>
Tampilan Tab 2 (tab_fragment_2.xml)
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="Tab 2"android:textAppearance="?android:attr/textAppearanceLarge"/></RelativeLayout>
Tab 1 (TabFragment1.java)
dapat juga digunakan dengan mengimplementasikan Librari ButterKnifepackage com.truiton.designsupporttabs;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class TabFragment1 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {return inflater.inflate(R.layout.tab_fragment_1, container, false);}}
Tab 1 (TabFragment1.java)
package com.truiton.designsupporttabs;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class TabFragment1 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.tab_fragment_1, container, false); ButterKnife.bind(this, view); return view;}}
Tab 2 (TabFragment2.java)
dapat juga digunakan dengan mengimplementasikan Librari ButterKnifepackage com.truiton.designsupporttabs;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class TabFragment2 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {return inflater.inflate(R.layout.tab_fragment_2, container, false);}}
Tab 2 (TabFragment2.java)
package com.truiton.designsupporttabs;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class TabFragment2 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.tab_fragment_2, container, false); ButterKnife.bind(this, view); return view;}}
Parent (MainActivity.java) ....(1)
import android.os.Bundle;import android.support.design.widget.TabLayout;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);setSupportActionBar(toolbar);TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());viewPager.setAdapter(adapter);viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {@Overridepublic void onTabSelected(TabLayout.Tab tab) {viewPager.setCurrentItem(tab.getPosition());}@Overridepublic void onTabUnselected(TabLayout.Tab tab) {}@Overridepublic void onTabReselected(TabLayout.Tab tab) {}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.menu_main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}
dapat juga di implementasikan ke fragment
Parent (MainActivity.java) ....(2)
Ketika kita menggunakan Lybrary ButterKnife itu akan menjadi lebih mudahimport android.os.Bundle;import android.support.design.widget.TabLayout;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_main, container, false);Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);AppCompatActivity activity = (AppCompatActivity) getActivity();activity.setSupportActionBar(toolbar);TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());viewPager.setAdapter(adapter);viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {@Overridepublic void onTabSelected(TabLayout.Tab tab) {viewPager.setCurrentItem(tab.getPosition());}@Overridepublic void onTabUnselected(TabLayout.Tab tab) {}@Overridepublic void onTabReselected(TabLayout.Tab tab) {}});return view;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.menu_main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}
Parent (MainActivity.java) ....(3)
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.io.IOException; import butterknife.Bind; import butterknife.ButterKnife; public class MainActivity extends Fragment implements TabLayout.OnTabSelectedListener { @Bind(R.id.toolbar) Toolbar toolbar; @Bind(R.id.sliding_tabs) TabLayout tabLayout; @Bind(R.id.pager) ViewPager viewpager; private PagerAdapter adapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_main, container, false); ButterKnife.bind(this, view); AppCompatActivity activity = (AppCompatActivity) getActivity(); activity.setSupportActionBar(toolbar); return view; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); setupTab(); setupPager(); } private void setupTab() { tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); tabLayout.setOnTabSelectedListener(this); } private void setupPager() { adapter = new ProfilAdapter (((AppCompatActivity) getActivity()).getFragmentManager(), tabLayout.getTabCount()); viewPager.setAdapter(adapter); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); } @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) { } }untuk dapat menampilkan pager, kita membutuhkan sebuah class adaptor
kelas adaptor (PagerAdaptor.java)
import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentStatePagerAdapter;public class PagerAdapter extends FragmentStatePagerAdapter {int mNumOfTabs;public PagerAdapter(FragmentManager fm, int NumOfTabs) {super(fm);this.mNumOfTabs = NumOfTabs;}@Overridepublic Fragment getItem(int position) {switch (position) {case 0:TabFragment1 tab1 = new TabFragment1();return tab1;case 1:TabFragment2 tab2 = new TabFragment2();return tab2;default:return null;}}@Overridepublic int getCount() {return mNumOfTabs;}}
untuk melakukan custom dapat dilihat lebih jauh di ->
https://guides.codepath.com/android/google-play-style-tabs-using-tablayout
No comments:
Post a Comment