使用非空的片段管理器。

huangapple go评论84阅读模式
英文:

Use not nullable fragment manager

问题

我想在我的片段中显示TabLayout,但它需要可空的supportFragmentManager

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val fragmentAdapter = MyPagerAdapter(activity?.supportFragmentManager)
    viewpager_main.adapter = fragmentAdapter

    tabs_main.setupWithViewPager(viewpager_main)
}

这是我的适配器:

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.mandarine.targetList.features.targets.list.TargetsFragment

class MyPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

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

    override fun getCount(): Int {
        return 3
    }

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

我如何重写我的val

val fragmentAdapter = activity?.supportFragmentManager?.let { MyPagerAdapter(it) }

java.lang.IllegalStateException: viewpager_main must not be null

还有,FragmentPagerAdapter已被弃用,也许需要使用新的东西?

英文:

I want to display my TabLayout in my fragment, but he want nullable supportFragmentManager:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val fragmentAdapter = MyPagerAdapter(activity?.supportFragmentManager)
        viewpager_main.adapter = fragmentAdapter

        tabs_main.setupWithViewPager(viewpager_main)
    }

Here is my adapter:

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.mandarine.targetList.features.targets.list.TargetsFragment

class MyPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

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

    override fun getCount(): Int {
        return 3
    }

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

How i can rewrite my val?

val fragmentAdapter = activity?.supportFragmentManager?.let { MyPagerAdapter(it) }

java.lang.IllegalStateException: viewpager_main must not be null

Also FragmentPagerAdapter is deprecated, mb need to use smth new?

答案1

得分: 1

Sure, here is the translated code snippet:

// getActivity()可能为null,而在片段准备过程中并即将准备就绪。通常发生在OnCreate中,在OnViewCreated中,视图已经生成,此处activity永远不会为null

// 在OnViewCreated中分配viewpager适配器

// 在类中全局定义以便在任何地方使用

private lateinit var viewpager_main: ViewPager
private lateinit var myPagerAdapter: MyPagerAdapter

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    viewpager_main = view.findViewById(R.id.viewpager_main)
    myPagerAdapter = MyPagerAdapter(activity!!.supportFragmentManager)
}

Try this and refer to the link below to understand the use of !! and ? in Kotlin:
Understanding !! and ? in Kotlin

英文:

getActivity() can be null while your fragment is in process of preparation and about to be ready. usually this happens in OnCreate while in OnViewCreated, view is already generated and activity will never be null here

Assign viewpager adapter in onViewCreated

Define both globally to use it anywhere in the class

    private lateinit var viewpager_main: ViewPager
    private lateinit var myPagerAdapter :MyPagerAdapter


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
 viewpager_main= view.findViewById(R.id.viewpager_main)
myPagerAdapter = MyPagerAdapter(activity!!.supportFragmentManager)
}

Try this

Check the to know about !! and ? in kotlin
https://stackoverflow.com/questions/44536114/the-difference-between-and-in-kotlin

huangapple
  • 本文由 发表于 2020年1月3日 19:34:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定