Advertisement
ali50mahmoud

Tablayout

Mar 14th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. ActionBar.TabListener tabListener = new ActionBar.TabListener() {
  2. public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
  3. // When the tab is selected, switch to the
  4. // corresponding page in the ViewPager.
  5. mViewPager.setCurrentItem(tab.getPosition());
  6. }
  7. ...
  8. };
  9.  
  10. /////////////////////////////////////////////////
  11. final ActionBar actionBar = getActionBar();
  12. ...
  13.  
  14. // Specify that tabs should be displayed in the action bar.
  15. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  16.  
  17. // Create a tab listener that is called when the user changes tabs.
  18. ActionBar.TabListener tabListener = new ActionBar.TabListener() {
  19. public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
  20. // show the given tab
  21. }
  22.  
  23. public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
  24. // hide the given tab
  25. }
  26.  
  27. public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
  28. // probably ignore this event
  29. }
  30. };
  31.  
  32. // Add 3 tabs, specifying the tab's text and TabListener
  33. for (int i = 0; i < 3; i++) {
  34. actionBar.addTab(
  35. actionBar.newTab()
  36. .setText("Tab " + (i + 1))
  37. .setTabListener(tabListener));
  38. }
  39.  
  40.  
  41.  
  42.  
  43. ////////////////////////////////////////////
  44. <TextView
  45. android:id="@+id/textView"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:layout_centerInParent="true"
  49. android:text="Tab 1"
  50. android:textAppearance="?android:attr/textAppearanceLarge"/>
  51. ///////////////////////////////////////
  52. public class Tab1 extends Fragment {
  53.  
  54. //Overriden method onCreateView
  55. @Override
  56. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  57.  
  58. //Returning the layout file after inflating
  59. //Change R.layout.tab1 in you classes
  60. return inflater.inflate(R.layout.tab1, container, false);
  61. }
  62. }
  63. ////////////////////////////////////////
  64. public class Pager extends FragmentStatePagerAdapter {
  65.  
  66. //integer to count number of tabs
  67. int tabCount;
  68.  
  69. //Constructor to the class
  70. public Pager(FragmentManager fm, int tabCount) {
  71. super(fm);
  72. //Initializing tab count
  73. this.tabCount= tabCount;
  74. }
  75.  
  76. //Overriding method getItem
  77. @Override
  78. public Fragment getItem(int position) {
  79. //Returning the current tabs
  80. switch (position) {
  81. case 0:
  82. Tab1 tab1 = new Tab1();
  83. return tab1;
  84. case 1:
  85. Tab2 tab2 = new Tab2();
  86. return tab2;
  87. case 2:
  88. Tab3 tab3 = new Tab3();
  89. return tab3;
  90. default:
  91. return null;
  92. }
  93. }
  94.  
  95. //Overriden method getCount to get the number of tabs
  96. @Override
  97. public int getCount() {
  98. return tabCount;
  99. }
  100. }
  101. /////////////////////////////////////////////main/////
  102. <android.support.v7.widget.Toolbar
  103. android:id="@+id/toolbar"
  104. android:layout_width="match_parent"
  105. android:layout_height="wrap_content"
  106. android:background="?attr/colorPrimary"
  107. android:minHeight="?attr/actionBarSize"
  108. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  109. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
  110.  
  111. <!-- our tablayout to display tabs -->
  112. <android.support.design.widget.TabLayout
  113. android:id="@+id/tabLayout"
  114. android:layout_width="match_parent"
  115. android:layout_height="wrap_content"
  116. android:background="?attr/colorPrimary"
  117. android:minHeight="?attr/actionBarSize"
  118. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
  119.  
  120. <!-- View pager to swipe views -->
  121. <android.support.v4.view.ViewPager
  122. android:id="@+id/pager"
  123. android:layout_width="match_parent"
  124. android:layout_height="fill_parent"/>
  125.  
  126. ////////////////////////////////////////main
  127. <android.support.v7.widget.Toolbar
  128. android:id="@+id/toolbar"
  129. android:layout_width="match_parent"
  130. android:layout_height="wrap_content"
  131. android:background="?attr/colorPrimary"
  132. android:minHeight="?attr/actionBarSize"
  133. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  134. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
  135.  
  136. <!-- our tablayout to display tabs -->
  137. <android.support.design.widget.TabLayout
  138. android:id="@+id/tabLayout"
  139. android:layout_width="match_parent"
  140. android:layout_height="wrap_content"
  141. android:background="?attr/colorPrimary"
  142. android:minHeight="?attr/actionBarSize"
  143. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
  144.  
  145. <!-- View pager to swipe views -->
  146. <android.support.v4.view.ViewPager
  147. android:id="@+id/pager"
  148. android:layout_width="match_parent"
  149. android:layout_height="fill_parent"/>
  150. ////////////////////////////////////////////////////////
  151.  
  152.  
  153. public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{
  154.  
  155. //This is our tablayout
  156. private TabLayout tabLayout;
  157.  
  158. //This is our viewPager
  159. private ViewPager viewPager;
  160.  
  161. @Override
  162. protected void onCreate(Bundle savedInstanceState) {
  163. super.onCreate(savedInstanceState);
  164. setContentView(R.layout.activity_main);
  165.  
  166. //Adding toolbar to the activity
  167. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  168. setSupportActionBar(toolbar);
  169.  
  170. //Initializing the tablayout
  171. tabLayout = (TabLayout) findViewById(R.id.tabLayout);
  172.  
  173. //Adding the tabs using addTab() method
  174. tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
  175. tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
  176. tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
  177. tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  178.  
  179. //Initializing viewPager
  180. viewPager = (ViewPager) findViewById(R.id.pager);
  181.  
  182. //Creating our pager adapter
  183. Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
  184.  
  185. //Adding adapter to pager
  186. viewPager.setAdapter(adapter);
  187.  
  188. //Adding onTabSelectedListener to swipe views
  189. tabLayout.setOnTabSelectedListener(this);
  190. }
  191.  
  192. @Override
  193. public void onTabSelected(TabLayout.Tab tab) {
  194. viewPager.setCurrentItem(tab.getPosition());
  195. }
  196.  
  197. @Override
  198. public void onTabUnselected(TabLayout.Tab tab) {
  199.  
  200. }
  201.  
  202. @Override
  203. public void onTabReselected(TabLayout.Tab tab) {
  204.  
  205. }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement