Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. package com.adeb.slide
  2.  
  3. import android.os.Bundle
  4. import android.view.*
  5. import com.google.android.material.snackbar.Snackbar
  6. import androidx.appcompat.app.AppCompatActivity
  7. import androidx.fragment.app.Fragment
  8. import androidx.fragment.app.FragmentManager
  9. import androidx.fragment.app.FragmentPagerAdapter
  10.  
  11. import kotlinx.android.synthetic.main.activity_main.*
  12. import kotlinx.android.synthetic.main.fragment_main.view.*
  13.  
  14.  
  15. class MainActivity : AppCompatActivity() {
  16.  
  17. /**
  18. * The [android.support.v4.view.PagerAdapter] that will provide
  19. * fragments for each of the sections. We use a
  20. * {@link FragmentPagerAdapter} derivative, which will keep every
  21. * loaded fragment in memory. If this becomes too memory intensive, it
  22. * may be best to switch to a
  23. * [android.support.v4.app.FragmentStatePagerAdapter].
  24. */
  25. private var mSectionsPagerAdapter: SectionsPagerAdapter? = null
  26.  
  27. override fun onCreate(savedInstanceState: Bundle?) {
  28. super.onCreate(savedInstanceState)
  29. setContentView(R.layout.activity_main)
  30.  
  31. // Create the adapter that will return a fragment for each of the three
  32. // primary sections of the activity.
  33. mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)
  34.  
  35. setSupportActionBar(toolbar)
  36.  
  37. //set toolbar title
  38. toolbar.title = "Slider"
  39. //set toolbar subtitle
  40. toolbar.subtitle = "Slide left/right to change..."
  41.  
  42. // Set up the ViewPager with the sections adapter.
  43. container.adapter = mSectionsPagerAdapter
  44.  
  45. }
  46.  
  47.  
  48. override fun onCreateOptionsMenu(menu: Menu): Boolean {
  49. // Inflate the menu; this adds items to the action bar if it is present.
  50. menuInflater.inflate(R.menu.menu_main, menu)
  51. return true
  52. }
  53.  
  54. override fun onOptionsItemSelected(item: MenuItem): Boolean {
  55. // Handle action bar item clicks here. The action bar will
  56. // automatically handle clicks on the Home/Up button, so long
  57. // as you specify a parent activity in AndroidManifest.xml.
  58. val id = item.itemId
  59.  
  60. if (id == R.id.action_settings) {
  61. return true
  62. }
  63.  
  64. return super.onOptionsItemSelected(item)
  65. }
  66.  
  67.  
  68. /**
  69. * A [FragmentPagerAdapter] that returns a fragment corresponding to
  70. * one of the sections/tabs/pages.
  71. */
  72. inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
  73.  
  74. override fun getItem(position: Int): Fragment {
  75. // getItem is called to instantiate the fragment for the given page.
  76. // Return a PlaceholderFragment (defined as a static inner class below).
  77. return PlaceholderFragment.newInstance(position + 1)
  78. }
  79.  
  80. override fun getCount(): Int {
  81. // Show 5 total pages.(we will use 5 pages so change it to 5)
  82. return 5
  83. }
  84. }
  85.  
  86. /**
  87. * A placeholder fragment containing a simple view.
  88. */
  89. class PlaceholderFragment : Fragment() {
  90.  
  91. override fun onCreateView(
  92. inflater: LayoutInflater, container: ViewGroup?,
  93. savedInstanceState: Bundle?
  94. ): View? {
  95. val rootView = inflater.inflate(R.layout.fragment_main, container, false)
  96. /*since our views are in fragment_main.xml which is inflated in rootView
  97. so we have to write rootView.oursomeview
  98. otherwise it will try to find the view in activity_main.xml so app will crash*/
  99. //handle swipe/slide
  100. if (arguments!!.getInt(ARG_SECTION_NUMBER) == 1){
  101. //set title to title_tv
  102. rootView.title_tv.text = "Title One"
  103. //set description to description_tv
  104. rootView.description_tv.text = "The long description of the Title One"
  105. }
  106. if (arguments!!.getInt(ARG_SECTION_NUMBER) == 2){
  107. //set title to title_tv
  108. rootView.title_tv.text = "Title Two"
  109. //set description to description_tv
  110. rootView.description_tv.text = "The long description of the Title Two"
  111. }
  112. if (arguments!!.getInt(ARG_SECTION_NUMBER) == 3){
  113. //set title to title_tv
  114. rootView.title_tv.text = "Title Three"
  115. //set description to description_tv
  116. rootView.description_tv.text = "The long description of the Title Three"
  117. }
  118. if (arguments!!.getInt(ARG_SECTION_NUMBER) == 4){
  119. //set title to title_tv
  120. rootView.title_tv.text = "Title Four"
  121. //set description to description_tv
  122. rootView.description_tv.text = "The long description of the Title Four"
  123. }
  124. if (arguments!!.getInt(ARG_SECTION_NUMBER) == 5){
  125. //set title to title_tv
  126. rootView.title_tv.text = "Title Five"
  127. //set description to description_tv
  128. rootView.description_tv.text = "The long description of the Title Five"
  129. }
  130. return rootView
  131. }
  132.  
  133. companion object {
  134. /**
  135. * The fragment argument representing the section number for this
  136. * fragment.
  137. */
  138. private val ARG_SECTION_NUMBER = "section_number"
  139.  
  140. /**
  141. * Returns a new instance of this fragment for the given section
  142. * number.
  143. */
  144. fun newInstance(sectionNumber: Int): PlaceholderFragment {
  145. val fragment = PlaceholderFragment()
  146. val args = Bundle()
  147. args.putInt(ARG_SECTION_NUMBER, sectionNumber)
  148. fragment.arguments = args
  149. return fragment
  150. }
  151. }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement