我如何在 Kotlin Fragment 中观察 Activity LiveData 类?

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

How can I observe Activity Live data class in Fragment Kotlin

问题

这是您提供的代码片段,以下是翻译的部分:

我一直在尝试在一个片段中观察这段代码但我无法理解如果我在普通的活动中使用它它可以工作但在片段中会出现错误

class HomeFragment : Fragment() {
    private lateinit var checkNetworkConnection: CheckNetworkConnection
    private lateinit var internetLayout: RelativeLayout
    private lateinit var noInternetLayout: RelativeLayout
    private lateinit var tryAgainButton: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // 为此片段充气布局
        return inflater.inflate(R.layout.fragment_home, container, false)
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        internetLayout = view.findViewById(R.id.internetLayout)
        noInternetLayout = view.findViewById(R.id.noInternetLayout)
        tryAgainButton = view.findViewById(R.id.try_again_button)
        tryAgainButton.setOnClickListener {
            callNetworkConnection()
        }
        callNetworkConnection()
        val url = ""
        val web = view.findViewById<View>(R.id.webView) as WebView
        web.settings.javaScriptEnabled = true
        web.settings.loadWithOverviewMode = true
        web.settings.useWideViewPort = true
        web.settings.builtInZoomControls = true
        web.settings.pluginState = WebSettings.PluginState.ON
        web.webViewClient = WebViewClient()
        web.loadUrl(url)
    }

    private fun callNetworkConnection() {
        checkNetworkConnection = CheckNetworkConnection()
        checkNetworkConnection.observe(this,{ isConnected ->
            if (isConnected){
                internetLayout.visibility = View.VISIBLE
                noInternetLayout.visibility = View.GONE
            } else {
                noInternetLayout.visibility = View.VISIBLE
                internetLayout.visibility = View.GONE
            }
        })
    }
}

这是您提到的错误信息:

以下函数都无法使用提供的参数调用:com.oreal.ng.CheckNetworkConnection中定义的公共构造函数(application:Application)com.oreal.ng.CheckNetworkConnection中定义的公共构造函数(connectivityManager:ConnectivityManager)

请帮我看看。

如果您需要更多帮助来解决这个错误,请提供更多上下文或具体问题的信息。

英文:

I have been trying to observe this code in a fragment but I am not getting it. If I use it on a normal activity it works but on fragment it brings errors.

class HomeFragment : Fragment() {
private lateinit var checkNetworkConnection: CheckNetworkConnection
private lateinit var internetLayout: RelativeLayout
private lateinit var noInternetLayout: RelativeLayout
private lateinit var tryAgainButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_home, container, false)

}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    internetLayout = view.findViewById(R.id.internetLayout)
    noInternetLayout = view.findViewById(R.id.noInternetLayout)
    tryAgainButton = view.findViewById(R.id.try_again_button)
    tryAgainButton.setOnClickListener {
        callNetworkConnection()
    }
    callNetworkConnection()
    val url = &quot;&quot;
    val web = view.findViewById&lt;View&gt;(R.id.webView) as WebView
    web.settings.javaScriptEnabled = true
    web.settings.loadWithOverviewMode = true
    web.settings.useWideViewPort = true
    web.settings.builtInZoomControls = true
    web.settings.pluginState = WebSettings.PluginState.ON
    web.webViewClient = WebViewClient()
    web.loadUrl(url)
}

private fun callNetworkConnection() {
    checkNetworkConnection = CheckNetworkConnection()
    checkNetworkConnection.observe(this,{ isConnected -&gt;
        if (isConnected){
            internetLayout.visibility = View.VISIBLE
            noInternetLayout.visibility = View.GONE
        } else {
            noInternetLayout.visibility = View.VISIBLE
            internetLayout.visibility = View.GONE
        }
    })

}

}

This the error I get:

> None of the following functions can be called with the arguments
> supplied: public constructor CheckNetworkConnection(application:
> Application) defined in com.oreal.ng.CheckNetworkConnection public
> constructor CheckNetworkConnection(connectivityManager:
> ConnectivityManager) defined in com.oreal.ng.CheckNetworkConnection

Please help me please

答案1

得分: 1

使用viewLifecycleOwner而不是this试试看。

英文:

Give viewLifecycleOwner a try instead of this.

答案2

得分: 0

使用requireContext()代替this

英文:

Instead of this try using requireContext()

huangapple
  • 本文由 发表于 2023年7月18日 05:41:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708244.html
匿名

发表评论

匿名网友

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

确定