将多个视图膨胀成一个片段

Inflate multiples views into a fragment

我正在开发我的应用程序,我需要列出像这样的小组视图:

(我不能 post 我的图片因为 Whosebug 的东西我不够出名)

IMAGE

片段已显示并正确应用,但我尝试显示的视图组没有。

我找不到如何使我的片段出现并使按钮起作用(但这不是现在的重点,所以我稍后再问这个问题)

class EditFragment : Fragment() {
lateinit var option : Spinner
lateinit var result : TextView
private lateinit var viewOfLayout: View
override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?

): View? {

    viewOfLayout =  inflater.inflate(R.layout.edit_fragment, container, false)

    val mainLayout = viewOfLayout.findViewById(R.id.scrollView1) as ScrollView
    //create a view to inflate the layout_item (the xml with the textView created before)
    val view = layoutInflater.inflate(R.layout.sensor_item, mainLayout, false)

    val options = arrayOf("A","V")

    option = view.spinner as Spinner
    result = view.textView7 as TextView
    option.adapter = ArrayAdapter<String>(activity,android.R.layout.simple_list_item_1,options)

return viewOfLayout
}

}

完整代码如下:https://pastebin.com/fYyXkupM

(我确定我的片段显示正常)

-> 我认为我的错误来自那里:

val view = layoutInflater.inflate(R.layout.sensor_item, mainLayout, false)



如果我用 "container" 替换 "mainLayout" 它确实有效,但它不是片段的 child。

谢谢。

您尚未从 viewOfLayout 初始化选项 Spinner。

好的,即使这有点具体,我找到了我的答案:

val view = layoutInflater.inflate(R.layout.sensor_item, container, false)
val insertPoint = viewOfLayout.findViewById(R.id.box_Parent) as LinearLayout
insertPoint.addView(view2, 0)