Advertisement
ncosentino

Tab Fragment Tutorial - Classes - SimpleTabDefinition.java

Oct 30th, 2013
1,592
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. /**
  2.  * Blog Post:
  3.  * http://devleader.ca/2013/11/04/fragments-tabbed-android-user-interface
  4.  *
  5.  * Layouts:
  6.  * activity_main.xml: http://pastebin.com/kj8M38Ze
  7.  * fragment_tabs.xml: http://pastebin.com/0b7HAeAK
  8.  * simple_tab.xml: http://pastebin.com/9SqLbS0X
  9.  *
  10.  * Classes:
  11.  * MainActivity.java: http://pastebin.com/eBv3H775
  12.  * SimpleTabDefinition.java: http://pastebin.com/wW3WCcy9
  13.  * TabDefinition.java: http://pastebin.com/KSRV2f3U
  14.  * TabsFragment.java: http://pastebin.com/9Asv7AVH
  15.  */
  16. package com.devleader.tab_fragment_tutorial;
  17.  
  18. import android.support.v4.app.Fragment;
  19. import android.view.Gravity;
  20. import android.view.LayoutInflater;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.widget.LinearLayout;
  24. import android.widget.TextView;
  25. import android.widget.LinearLayout.LayoutParams;
  26.  
  27. /**
  28.  * A class that defines a simple tab.
  29.  */
  30. public class SimpleTabDefinition extends TabDefinition {
  31.     //
  32.     // Fields
  33.     //
  34.     private final int _tabTitleResourceId;
  35.     private final int _tabTitleViewId;
  36.     private final int _tabLayoutId;
  37.     private final Fragment _fragment;
  38.    
  39.     //
  40.     // Constructors
  41.     //
  42.     /**
  43.      * The constructor for {@link SimpleTabDefinition}.
  44.      * @param tabContentViewId The layout ID of the contents to use when the tab is active.
  45.      * @param tabLayoutId The ID of the layout to use when inflating the tab {@link View}.
  46.      * @param tabTitleResourceId The string resource ID for the title of the tab.
  47.      * @param tabTitleViewId The layout ID for the title of the tab.
  48.      * @param fragment The {@link Fragment} used when the tab is active.
  49.      */
  50.     public SimpleTabDefinition(int tabContentViewId, int tabLayoutId, int tabTitleResourceId, int tabTitleViewId, Fragment fragment) {
  51.         super(tabContentViewId);
  52.        
  53.         _tabLayoutId = tabLayoutId;
  54.         _tabTitleResourceId = tabTitleResourceId;
  55.         _tabTitleViewId = tabTitleViewId;
  56.         _fragment = fragment;
  57.     }
  58.    
  59.     //
  60.     // Exposed Members
  61.     //
  62.     @Override
  63.     public Fragment getFragment() {
  64.         return _fragment;
  65.     }
  66.    
  67.     @Override
  68.     public View createTabView(LayoutInflater inflater, ViewGroup tabsView) {
  69.         // we need to inflate the view based on the layout id specified when
  70.         // this instance was created.
  71.         View indicator = inflater.inflate(
  72.             _tabLayoutId,
  73.             tabsView,
  74.             false);
  75.        
  76.         // set up the title of the tab. this will populate the text with the
  77.         // string defined by the resource passed in when this instance was
  78.         // created. the text will also be centered within the title control.
  79.         TextView titleView = (TextView)indicator.findViewById(_tabTitleViewId);
  80.         titleView.setText(_tabTitleResourceId);
  81.         titleView.setGravity(Gravity.CENTER);
  82.        
  83.         // ensure the control we're inflating is layed out properly. this will
  84.         // cause our tab titles to be placed evenly weighted across the top.
  85.         LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
  86.             LayoutParams.WRAP_CONTENT,
  87.             LayoutParams.WRAP_CONTENT);
  88.         layoutParams.weight = 1;
  89.         indicator.setLayoutParams(layoutParams);
  90.        
  91.         return indicator;
  92.     }
  93. }
Advertisement
Comments
  • ncosentino
    237 days
    # text 0.18 KB | 0 0
    1. Remember to check out my blog: https://www.devleader.ca
    2. Subscribe to my newsletter at: https://www.devleader.ca/newsletter
    3. Check out my videos at: https://www.youtube.com/devleader
Add Comment
Please, Sign In to add comment
Advertisement