【问题标题】:How can I change different URL or view in tabbed activity?如何在选项卡式活动中更改不同的 URL 或视图?
【发布时间】:2024-01-17 06:12:01
【问题描述】:

我是 Android kotlin 开发的新手。我想在我的应用中创建一个选项卡式活动来显示不同的静态 HTML 页面。

我现在可以在我的应用中设置 1 个 URL。但是当我更改或点击另一个选项卡时,它会显示类似的视图或 URL。


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.support.v4.app.Fragment
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.webkit.WebView
import xxx.abcabc.xxx.R
import android.util.Log


class PlaceholderFragment : Fragment() {

    private lateinit var pageViewModel: PageViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        pageViewModel = ViewModelProviders.of(this).get(PageViewModel::class.java).apply {
            setIndex(arguments?.getInt(ARG_SECTION_NUMBER) ?: 1)
        }


    }

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_left_menu_policies, container, false)

        var webTnc: WebView = root.findViewById(R.id.myWebView)
        webTnc.loadUrl("https://www.abcxyz.com/abcxyz/abcxyz_webpage/tnc.html")


        return root
    }

    companion object {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private const val ARG_SECTION_NUMBER = "section_number"

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        @JvmStatic
        fun newInstance(sectionNumber: Int): PlaceholderFragment {
            return PlaceholderFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_SECTION_NUMBER, sectionNumber)
                }
            }
        }

    }
}

如何检测 sectionNumber 之类的东西

if(sectionNumber == 0)
{
 var webTnc: WebView = root.findViewById(R.id.myWebView)
    webTnc.loadUrl("https://www.abcxyz.com/abcxyz/abcxyz_webpage/tnc.html")
}
else
{
var webTnc: WebView = root.findViewById(R.id.myWebView)
    webTnc.loadUrl("https://www.URL2.com/URL2.html")
}

请帮忙。谢谢。

已编辑

【问题讨论】:

  • 在您的适配器中更改片段时,通过捆绑传递位置。

标签: android kotlin fragment android-tabbed-activity


【解决方案1】:

我看到您将sectionNumber 放入片段中的捆绑参数中。所以你可以使用arguments 来取回数据。

试试这个:

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val root = inflater.inflate(R.layout.fragment_left_menu_policies, container, false)
    val sectionNumber = arguments?.getInt(ARG_SECTION_NUMBER) ?: 1
    if (sectionNumber == 1) {
        var webTnc: WebView = root.findViewById(R.id.myWebView)
        webTnc.loadUrl("https://www.abcxyz.com/abcxyz/abcxyz_webpage/tnc.html")
    } else {
        var webTnc: WebView = root.findViewById(R.id.myWebView)
        webTnc.loadUrl("https://www.URL2.com/URL2.html")
    }


    return root
}

【讨论】:

  • 我尝试应用您的建议,但收到错误“未解决的参考:sectionNumber”。有什么想法吗?
  • 我犯了一个愚蠢的错误。我通过答案更正了。使用这条线val sectionNumber = arguments?.getInt(ARG_SECTION_NUMBER) ?: 1
  • 嗨 Birju,应用您的代码后,两个选项卡将显示相同的结果。我可以知道我做错了哪一部分吗?您可以参考上面所附的屏幕。
  • 当我将if (sectionNumber == 0) { 更改为if (sectionNumber == 1) { 时,它终于起作用了。
最近更新 更多