代码之家  ›  专栏  ›  技术社区  ›  Tony

公共建筑Kotlin

  •  -1
  • Tony  · 技术社区  · 6 年前

    在活动A中,我有这个

     override fun onTabSelected(p0: TabLayout.Tab?) {
                    val position = p0?.position
                    when (position) {
                        0 ->
                            supportFragmentManager.beginTransaction().replace(
                                R.id.frame_container,
                                CallFragment(mWorkOrder.title,mWorkOrder.description,mWorkOrder.location.name,mWorkOrder.status)
                            ).addToBackStack(null).commit()
                    }
                }
    

    在片段中

    class CallFragment : BaseFragment() {
    
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            val view = inflater.inflate(R.layout.fragment_create, container, false)
            return view
        }
    
    
        class CallFragment(mTitle: String, mDescription: String, mLocation: String, mStatus: String) {
    
            var title: String
            var description: String
            var location: String
            var status: String
    
            init {
                title = mTitle
                description = mDescription
                location = mLocation
                status = mStatus    
            }
        }
    

    活动A中的错误

    Too many arguments for public constructor CallFragment() defined in xxx
    

    我在callFragment中用相同数量的参数定义了一个新的构造函数,但该方法似乎没有被调用。任何帮助都将不胜感激。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Son Truong    6 年前

    在Android中,如果要将参数传递给片段类,建议使用工厂方法。

    class CallFragment : BaseFragment() {
    
        override fun onCreateView(
                inflater: LayoutInflater, container: ViewGroup?,
                savedInstanceState: Bundle?
        ): View? {
            val view = inflater.inflate(R.layout.fragment_create, container, false)
            // Get arguments' value
            val title = arguments!!.getString(ARGUMENT_TITLE)
            val description = arguments!!.getString(ARGUMENT_DESCRIPTION)
            val location = arguments!!.getString(ARGUMENT_LOCATION)
            val status = arguments!!.getString(ARGUMENT_STATUS)
            return view
        }
    
        companion object {
            private const val ARGUMENT_TITLE = "ARGUMENT_TITLE"
            private const val ARGUMENT_DESCRIPTION = "ARGUMENT_DESCRIPTION"
            private const val ARGUMENT_LOCATION = "ARGUMENT_LOCATION"
            private const val ARGUMENT_STATUS = "ARGUMENT_STATUS"
    
            /**
             * Using this factory method to create an instance of this fragment based on given arguments.
             */
            fun newInstance(title: String, description: String, location: String, status: String): CallFragment {
                return CallFragment().apply {
                    val args = Bundle().apply {
                        putString(ARGUMENT_TITLE, title)
                        putString(ARGUMENT_DESCRIPTION, description)
                        putString(ARGUMENT_LOCATION, location)
                        putString(ARGUMENT_STATUS, status)
                    }
                    arguments = args
                }
            }
        }
    }
    

    在活动中使用下面的片段

    override fun onTabSelected(p0: TabLayout.Tab?) {
                    val position = p0?.position
                    when (position) {
                        0 ->
                            supportFragmentManager.beginTransaction().replace(
                                R.id.frame_container,
                                CallFragment.newInstance(mWorkOrder.title,mWorkOrder.description,mWorkOrder.location.name,mWorkOrder.status)
                            ).addToBackStack(null).commit()
                    }
                }