Advertisement
Guest User

TabActivity template

a guest
Nov 8th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. package com.company.me.navbartest;
  2.  
  3. import android.support.design.widget.TabLayout;
  4. import android.support.design.widget.FloatingActionButton;
  5. import android.support.design.widget.Snackbar;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.support.v7.widget.Toolbar;
  8.  
  9. import android.support.v4.app.Fragment;
  10. import android.support.v4.app.FragmentManager;
  11. import android.support.v4.app.FragmentPagerAdapter;
  12. import android.support.v4.view.ViewPager;
  13. import android.os.Bundle;
  14. import android.view.LayoutInflater;
  15. import android.view.Menu;
  16. import android.view.MenuItem;
  17. import android.view.View;
  18. import android.view.ViewGroup;
  19.  
  20. import android.widget.TextView;
  21.  
  22. public class MainActivity extends AppCompatActivity {
  23.  
  24.     /**
  25.      * The {@link android.support.v4.view.PagerAdapter} that will provide
  26.      * fragments for each of the sections. We use a
  27.      * {@link FragmentPagerAdapter} derivative, which will keep every
  28.      * loaded fragment in memory. If this becomes too memory intensive, it
  29.      * may be best to switch to a
  30.      * {@link android.support.v4.app.FragmentStatePagerAdapter}.
  31.      */
  32.     private SectionsPagerAdapter mSectionsPagerAdapter;
  33.  
  34.     /**
  35.      * The {@link ViewPager} that will host the section contents.
  36.      */
  37.     private ViewPager mViewPager;
  38.  
  39.     @Override
  40.     protected void onCreate(Bundle savedInstanceState) {
  41.         super.onCreate(savedInstanceState);
  42.         setContentView(R.layout.activity_main);
  43.  
  44.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  45.         setSupportActionBar(toolbar);
  46.         // Create the adapter that will return a fragment for each of the three
  47.         // primary sections of the activity.
  48.         mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  49.  
  50.         // Set up the ViewPager with the sections adapter.
  51.         mViewPager = (ViewPager) findViewById(R.id.container);
  52.         mViewPager.setAdapter(mSectionsPagerAdapter);
  53.  
  54.         TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
  55.         tabLayout.setupWithViewPager(mViewPager);
  56.  
  57.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  58.         fab.setOnClickListener(new View.OnClickListener() {
  59.             @Override
  60.             public void onClick(View view) {
  61.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  62.                         .setAction("Action", null).show();
  63.             }
  64.         });
  65.  
  66.     }
  67.  
  68.  
  69.     @Override
  70.     public boolean onCreateOptionsMenu(Menu menu) {
  71.         // Inflate the menu; this adds items to the action bar if it is present.
  72.         getMenuInflater().inflate(R.menu.menu_main, menu);
  73.         return true;
  74.     }
  75.  
  76.     @Override
  77.     public boolean onOptionsItemSelected(MenuItem item) {
  78.         // Handle action bar item clicks here. The action bar will
  79.         // automatically handle clicks on the Home/Up button, so long
  80.         // as you specify a parent activity in AndroidManifest.xml.
  81.         int id = item.getItemId();
  82.  
  83.         //noinspection SimplifiableIfStatement
  84.         if (id == R.id.action_settings) {
  85.             return true;
  86.         }
  87.  
  88.         return super.onOptionsItemSelected(item);
  89.     }
  90.  
  91.     /**
  92.      * A placeholder fragment containing a simple view.
  93.      */
  94.     public static class PlaceholderFragment extends Fragment {
  95.         /**
  96.          * The fragment argument representing the section number for this
  97.          * fragment.
  98.          */
  99.         private static final String ARG_SECTION_NUMBER = "section_number";
  100.  
  101.         public PlaceholderFragment() {
  102.         }
  103.  
  104.         /**
  105.          * Returns a new instance of this fragment for the given section
  106.          * number.
  107.          */
  108.         public static PlaceholderFragment newInstance(int sectionNumber) {
  109.             PlaceholderFragment fragment = new PlaceholderFragment();
  110.             Bundle args = new Bundle();
  111.             args.putInt(ARG_SECTION_NUMBER, sectionNumber);
  112.             fragment.setArguments(args);
  113.             return fragment;
  114.         }
  115.  
  116.         @Override
  117.         public View onCreateView(LayoutInflater inflater, ViewGroup container,
  118.                                  Bundle savedInstanceState) {
  119.             View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  120.             TextView textView = (TextView) rootView.findViewById(R.id.section_label);
  121.             textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
  122.             return rootView;
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
  128.      * one of the sections/tabs/pages.
  129.      */
  130.     public class SectionsPagerAdapter extends FragmentPagerAdapter {
  131.  
  132.         public SectionsPagerAdapter(FragmentManager fm) {
  133.             super(fm);
  134.         }
  135.  
  136.         @Override
  137.         public Fragment getItem(int position) {
  138.             // getItem is called to instantiate the fragment for the given page.
  139.             // Return a PlaceholderFragment (defined as a static inner class below).
  140.             return PlaceholderFragment.newInstance(position + 1);
  141.         }
  142.  
  143.         @Override
  144.         public int getCount() {
  145.             // Show 3 total pages.
  146.             return 3;
  147.         }
  148.  
  149.         @Override
  150.         public CharSequence getPageTitle(int position) {
  151.             switch (position) {
  152.                 case 0:
  153.                     return "SECTION 1";
  154.                 case 1:
  155.                     return "SECTION 2";
  156.                 case 2:
  157.                     return "SECTION 3";
  158.             }
  159.             return null;
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement