简体   繁体   中英

How to refresh data in fragment in ViewPager2 with an interface

I'm trying to refresh the data in my fragment every time the user clicks on it in the Bottom Menu Navigation. I already wrote an interface which gets called each time the fragment gets selected by the user The problem is that the method inside my fragment has no access to the view of the fragment (I guess):

MainMenu

    viewPager = findViewById(R.id.frame_container)
    viewPager!!.offscreenPageLimit = 5
    viewPager!!.orientation = ViewPager2.ORIENTATION_HORIZONTAL
    viewPager!!.adapter = pageAdapter
    viewPager!!.currentItem = 0
    viewPager!!.isUserInputEnabled = false
    viewPager!!.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            if(position == 2) {
                val mFragment = ListFragment()
                mFragment.ready(this@MainMenu)

            }
        }

    })

The Interface "Ready":

interface ReadyInterface {
    fun ready(activity: FragmentActivity?)
}

and the ListFragment:

class ListFragment: Fragment(), ReadyInterface {

    var mView : View? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_list, container, false)
        this.mView = view

        val user = FirebaseAuth.getInstance().currentUser
        return if (user != null) {

        view
        } else {
        val unregisteredView = inflater.inflate(R.layout.fragment_unregistred, container, false)
        val registerNow = unregisteredView.findViewById<TextView>(R.id.textview_registernow)
        unregisteredView
    }


override fun ready(activity: FragmentActivity?) {

    testText = mView!!.findViewById(R.id.test_text)
    testText.text = "Test Text here"
    Toast.makeText(activity!!.applicationContext,"Test",Toast.LENGTH_LONG).show()
}

This code here crashes with a "kotlin.KotlinNullPointerException" on line "testText.text =...."

So I guess the fun ready hasn't got access to the view of my fragment because of the fragments lifecycle, am I right? How could I fix this?

There is no need to implement interface here. You can directly make ready() a member function of ListFragment and call it from onPageSelected(position: Int) in MainMenuActivity . Currently, the interface method is being called before onCreateView() causing it to throw null pointer exception as the view is not initialised yet.

You are creating a new instance of ListFragment() while calling ready on it. This new fragment is not attached to the ViewPager. This new instance also has not gone through any lifecycle methods, therefore, it's view is not yet initialized. Due to this, you get a NullPointerException while calling methods on a view. To solve this you can instead fetch the current fragment from the ViewPager and call your method on it.

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