Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.adeb.slide
- import android.os.Bundle
- import android.view.*
- import com.google.android.material.snackbar.Snackbar
- import androidx.appcompat.app.AppCompatActivity
- import androidx.fragment.app.Fragment
- import androidx.fragment.app.FragmentManager
- import androidx.fragment.app.FragmentPagerAdapter
- import kotlinx.android.synthetic.main.activity_main.*
- import kotlinx.android.synthetic.main.fragment_main.view.*
- class MainActivity : AppCompatActivity() {
- /**
- * The [android.support.v4.view.PagerAdapter] that will provide
- * fragments for each of the sections. We use a
- * {@link FragmentPagerAdapter} derivative, which will keep every
- * loaded fragment in memory. If this becomes too memory intensive, it
- * may be best to switch to a
- * [android.support.v4.app.FragmentStatePagerAdapter].
- */
- private var mSectionsPagerAdapter: SectionsPagerAdapter? = null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- // Create the adapter that will return a fragment for each of the three
- // primary sections of the activity.
- mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)
- setSupportActionBar(toolbar)
- //set toolbar title
- toolbar.title = "Slider"
- //set toolbar subtitle
- toolbar.subtitle = "Slide left/right to change..."
- // Set up the ViewPager with the sections adapter.
- container.adapter = mSectionsPagerAdapter
- }
- override fun onCreateOptionsMenu(menu: Menu): Boolean {
- // Inflate the menu; this adds items to the action bar if it is present.
- menuInflater.inflate(R.menu.menu_main, menu)
- return true
- }
- override fun onOptionsItemSelected(item: MenuItem): Boolean {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- val id = item.itemId
- if (id == R.id.action_settings) {
- return true
- }
- return super.onOptionsItemSelected(item)
- }
- /**
- * A [FragmentPagerAdapter] that returns a fragment corresponding to
- * one of the sections/tabs/pages.
- */
- inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
- override fun getItem(position: Int): Fragment {
- // getItem is called to instantiate the fragment for the given page.
- // Return a PlaceholderFragment (defined as a static inner class below).
- return PlaceholderFragment.newInstance(position + 1)
- }
- override fun getCount(): Int {
- // Show 5 total pages.(we will use 5 pages so change it to 5)
- return 5
- }
- }
- /**
- * A placeholder fragment containing a simple view.
- */
- class PlaceholderFragment : Fragment() {
- override fun onCreateView(
- inflater: LayoutInflater, container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- val rootView = inflater.inflate(R.layout.fragment_main, container, false)
- /*since our views are in fragment_main.xml which is inflated in rootView
- so we have to write rootView.oursomeview
- otherwise it will try to find the view in activity_main.xml so app will crash*/
- //handle swipe/slide
- if (arguments!!.getInt(ARG_SECTION_NUMBER) == 1){
- //set title to title_tv
- rootView.title_tv.text = "Title One"
- //set description to description_tv
- rootView.description_tv.text = "The long description of the Title One"
- }
- if (arguments!!.getInt(ARG_SECTION_NUMBER) == 2){
- //set title to title_tv
- rootView.title_tv.text = "Title Two"
- //set description to description_tv
- rootView.description_tv.text = "The long description of the Title Two"
- }
- if (arguments!!.getInt(ARG_SECTION_NUMBER) == 3){
- //set title to title_tv
- rootView.title_tv.text = "Title Three"
- //set description to description_tv
- rootView.description_tv.text = "The long description of the Title Three"
- }
- if (arguments!!.getInt(ARG_SECTION_NUMBER) == 4){
- //set title to title_tv
- rootView.title_tv.text = "Title Four"
- //set description to description_tv
- rootView.description_tv.text = "The long description of the Title Four"
- }
- if (arguments!!.getInt(ARG_SECTION_NUMBER) == 5){
- //set title to title_tv
- rootView.title_tv.text = "Title Five"
- //set description to description_tv
- rootView.description_tv.text = "The long description of the Title Five"
- }
- return rootView
- }
- companion object {
- /**
- * The fragment argument representing the section number for this
- * fragment.
- */
- private val ARG_SECTION_NUMBER = "section_number"
- /**
- * Returns a new instance of this fragment for the given section
- * number.
- */
- fun newInstance(sectionNumber: Int): PlaceholderFragment {
- val fragment = PlaceholderFragment()
- val args = Bundle()
- args.putInt(ARG_SECTION_NUMBER, sectionNumber)
- fragment.arguments = args
- return fragment
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement