Advertisement
Guest User

Untitled

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