Kotlin:在onCreateView中创建内容

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kotlin:在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)

以上是关于Kotlin:在onCreateView中创建内容的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Kotlin 常用代码中创建动态代理?

在 Kotlin 中创建 Spring 的 ParameterizedTypeReference 实例

如何从枚举在 Kotlin 中创建编译时常量?

emre1512 在 Kotlin 中创建 Noty RelativeLayout

如何在 Kotlin 中创建一个打开新活动(Android Studio)的按钮?

在 kotlin 中创建泛型类的新实例的正确方法是啥?