如何更改不同的 URL 或在选项卡 activity 中查看?

How can I change different URL or view in tabbed activity?

我是 Android kotlin 开发的新手。我想在我的应用程序中创建一个选项卡 activity 以显示不同的静态 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")
}

请帮忙。谢谢。

已编辑

我看到您将 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
}