简体   繁体   中英

How can i pass a variable from my adapter to a fragment i instantiate in Kotlin

I followed a tutorial on getting started with tabhost and fragments, there is the link: https://medium.com/@eijaz/getting-started-with-tablayout-in-android-kotlin-bb7e21783761

But I got a problem, I want to send a value activityID to a fragment that my tabhost contains and I don't know how to do it.

I found on the internet that there is a method setArgument but no one of my objects contains this method.

Did I do something wrong ? Or could someone explain to me how to send my value from my ActivityTabHost to my fragment?

This is my ActivityTabHost.kt

import android.os.Bundle
import training.gutai.apps.gutai.R
import kotlinx.android.synthetic.main.activity_tabhost.*
import android.support.v7.app.AppCompatActivity
import android.util.Log
import training.gutai.apps.gutai.models.TabHostAdapter


class ActivityTabHost : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tabhost)

        val activityID = intent.extras.getInt("id")
        Log.d("Id", activityID.toString())
        val fragmentAdapter = TabHostAdapter(supportFragmentManager, activityID)
        viewpager_main.adapter = fragmentAdapter
        tabs_main.setupWithViewPager(viewpager_main)
    }
}

And this is my TabHostAdapter.kt :

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import training.gutai.apps.gutai.viewControllers.FirstFragment
import training.gutai.apps.gutai.viewControllers.SecondFragment

class TabHostAdapter(fm: FragmentManager, activityID : Int) :         FragmentPagerAdapter(fm) {

    val fm : FragmentManager = fm

    override fun getItem(position: Int): Fragment {
        return when (position) {
            0 -> {
                FirstFragment()
            }
            else -> {
                SecondFragment()
            }
        }
    }

    override fun getCount(): Int {
        return 2
    }

    override fun getPageTitle(position: Int): CharSequence {
        return when (position) {
            0 -> "First Tab"
            else -> "Second Tab"
        }
    }
}

The FragmentPagerAdapter is not having the setArguments method. The adapter is only holding the fragments. The fragments are having the setArguments method.

In your TabHostAdapter you can set the value activityID to the desired fragment ( FirstFragment or SecondFragment ) using the setArguments method. Something like the following, in your getItem method, before returning the fragment:

val fragment = FirstFragment()
val args = Bundle()
args.putInt("activityID", activityID)
fragment.setArguments(args)    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM