Kotlin:在 onCreateView 中创建内容

Kotlin : create content in onCreateView

由于我的函数 getInfoTrafic(),我正在生成一些内容。但是当我在我的屏幕上显示它时,我的屏幕被加载,然后(大约一秒钟),我的内容就生成了。

为什么?这对于一个片段来说是正常的吗?是否可以如我所愿?

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.infos_trafic, container, false)
    getInfosTrafic(view)
    return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
}

这些是我的功能。

private fun getInfosTrafic(view : View) {
    val resultActus = Fuel.get("MYAPI").responseJson { request, response, result ->
        println("Reponse : "+response.toString())
        result.fold({
            json ->
            Log.i(TAG, (json.content))
            parseInfosTrafic(json.content, view)
        }, {
            err ->
            Log.i("error", err.toString())
        })
    }
}

这个正在将我的元素添加到我的布局中

    private fun parseInfosTrafic(json: String, view : View): ArrayList<String> {    
        val array = JSONArray(json)
        val infos : ArrayList<String> = ArrayList<String>()
        val hashMap = HashMap<Int, JSONObject>()
        val llScrollInfoTraficFav = view.findViewById<LinearLayout>(R.id.llScrollInfoTraficFav)
        val llScrollInfoTraficTL= view.findViewById<LinearLayout>(R.id.llScrollInfoTraficTL)

        (0 until array.length()).forEach{
          for(i in 0..3){
            val evenOdd = if (i %2 == 0) "even" else "odd"

            val item = array.getJSONObject(it)
            val ...

            val mpLinearLayoutFirst = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
            val LinearLayoutFirst = LinearLayout(view.context)
            LinearLayoutFirst.layoutParams = mpLinearLayoutFirst
            LinearLayoutFirst.orientation = LinearLayout.VERTICAL
            LinearLayoutFirst.tag = "result"
            mpLinearLayoutFirst.bottomMargin = 5.toPx()

            val mpLinearLayoutSecond = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
            val linearLayoutSecond = LinearLayout(view.context)
            linearLayoutSecond.layoutParams = mpLinearLayoutSecond
            linearLayoutSecond.orientation = LinearLayout.VERTICAL
            linearLayoutSecond.setPadding(35.toPx(),35.toPx(),35.toPx(),35.toPx())
            linearLayoutSecond.background = resources.getDrawable(R.drawable.whit_bg_and_shadow, activity?.theme)
            linearLayoutSecond.tag = "visibleNotChangeable"

            LinearLayoutFirst.addView(linearLayoutSecond)

            val mpLinearLayoutThird = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
            val linearLayoutThird = LinearLayout(view.context)

            linearLayoutSecond.addView(linearLayoutThird)

            val rubikMedium = ResourcesCompat.getFont(view.context, R.font.rubik_medium)

            val wwRes = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
            val dateView = TextView(view.context)
            dateView.setTextAppearance(R.style.blueItalic)
            dateView.layoutParams = wwRes
            dateView.typeface = rubikMedium
            dateView.text = date
            dateView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)

            val title = TextView(view.context)
            title.setTextAppearance(R.style.citySearch)
            title.layoutParams = wwRes
            title.typeface = rubikMedium
            title.text = titleJson
            title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)

            linearLayoutThird.addView(dateView)
            linearLayoutThird.addView(title)

            val linearLayoutHidden = LinearLayout(view.context)
            val mpLinearLayoutHidden = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
            linearLayoutHidden.layoutParams = mpLinearLayoutHidden
            linearLayoutHidden.setPadding(35.toPx(),35.toPx(),35.toPx(),35.toPx())
            linearLayoutHidden.orientation = LinearLayout.VERTICAL
            linearLayoutHidden.background = resources.getDrawable(R.drawable.gray_bg, activity?.theme)
            linearLayoutHidden.visibility = LinearLayout.GONE
            linearLayoutHidden.tag = "toToggle"

            val layoutResume = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
            val resume = WebView(view.context)
            resume.layoutParams = layoutResume
            resume.settings
            resume.setBackgroundColor(Color.TRANSPARENT)
            resume.loadData(content, "text/html; charset=utf-8", "utf-8")

            linearLayoutHidden.addView(resume)

            val layoutButton =  LinearLayout.LayoutParams(40.toPx(),  40.toPx())
            val buttonToggle = Button(view.context)
            layoutButton.topMargin = (-20).toPx()
            layoutButton.gravity = Gravity.CENTER
            buttonToggle.layoutParams = layoutButton
            buttonToggle.background = resources.getDrawable(R.drawable.arrow_down_blue_circle, activity?.theme)
            buttonToggle.tag = "toToggleButton"

            buttonToggle.setOnClickListener {
                val viewParent = it.parent
                if (viewParent is LinearLayout) {
                    val taggedView = viewParent.findViewWithTag<View>("toToggle")

                    taggedView.visibility = when {
                        taggedView.visibility == View.GONE -> View.VISIBLE
                        else -> View.GONE
                    }
                }

            }

            LinearLayoutFirst.addView(linearLayoutHidden)
            LinearLayoutFirst.addView(buttonToggle)

            if(evenOdd == "even"){
                llScrollInfoTraficFav.addView(LinearLayoutFirst)
            }else{
                llScrollInfoTraficTL.addView(LinearLayoutFirst)
            }

        }
        return infos

    }

您不应在创建视图之前对 UI 线程进行操作。 onCreateView() 方法应该只用于扩充布局和返回视图。 返回视图并且系统显示 UI 后,您可以开始使用 Fragment 业务逻辑。将您的代码移到 onViewCreated() 方法下,避免在主线程(Android 更新 UI 的线程)

中进行长时间操作
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
   return inflater.inflate(R.layout.infos_trafic, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    getInfosTrafic(view)
}