簡體   English   中英

在 Tablayout 中的不同 Fragment 之間切換時如何保存 Webview 狀態?

[英]How to save Webview state when switching between different Fragments in a Tablayout?

我正在制作一個非常簡單的 webview 應用程序,它將網站的不同子頁面放置在不同的選項卡下(使用 tablayout),即消息、訂單等。各個 webviews 是使用片段創建的。 這個想法是用戶可以在頁面之間快速來回切換。 在測試時,當我直接切換到我所在的選項卡旁邊的選項卡然后返回時,該頁面與我離開選項卡時的狀態完全相同。 但是,我切換到與我當前所在的選項卡相距一個選項卡以外的選項卡,然后切換回上一個選項卡,頁面被重新加載。 我希望在用戶切換到任何其他選項卡時記住頁面狀態。 如何才能做到這一點?

TL; DR:如何在使用 tablayout 的選項卡之間切換時保留 webview 狀態,其中每個片段顯示不同的 webview?

作為參考,片段之一的代碼:

class BrowseFragment : Fragment() {
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    val myView = inflater.inflate(R.layout.fragment_browse, container, false)

    val webViewBrowse = myView.findViewById(R.id.webViewBrowse) as WebView;
    webViewBrowse.setWebViewClient(WebViewClient())
    webViewBrowse.loadUrl("https://websitehome.page")
    webViewBrowse.settings.setJavaScriptEnabled(true)

    return myView
}

}

由 SectionsPagerAdapter.tk 控制:

class SectionsPagerAdapter(private val context: Context, fm: FragmentManager) :
    FragmentPagerAdapter(fm) {

override fun getItem(position: Int): Fragment {
    // getItem is called to instantiate the fragment for the given page.

    return when (position) {
        0 -> {
            BrowseFragment()
        }
        1 -> {
            SecondFragment()
        }
        2 -> {
            ThirdFragment()
        }
        3 -> {
            FourthFragment()
        }
        else -> BrowseFragment()
    }

}

override fun getPageTitle(position: Int): CharSequence? {
    return context.resources.getString(TAB_TITLES[position])
}

override fun getCount(): Int {
    // Show 4 total pages.
    return 4
}

}

不推薦使用尋呼機適配器

使用 ViewPager2

首先將 offscreenPageLimit 設置為您的 viewPager2

//YOUR_ID
  yourViewPager2.offscreenPageLimit =3

使用 PagerStateAdapter

class SectionsPagerAdapter(mActivity: FragmentActivity) : FragmentStateAdapter(mActivity) {

private var mBrowseFragment: BrowseFragment? = null
private var mSecondFragment: SecondFragment? = null
private var mThirdFragment: ThirdFragment? = null
private var mFourthFragment: FourthFragment? = null

override fun getItemCount() = 4

override fun createFragment(position: Int): Fragment {
  return when (position) {
        0 -> {
            if (mBrowseFragment != null) {
                mBrowseFragment!!
            } else {
                mBrowseFragment = BrowseFragment()
                mBrowseFragment!!
            }
        }
        1 -> {
            if (mSecondFragment != null) {
                mSecondFragment!!
            } else {
                mSecondFragment = SecondFragment()
                mSecondFragment!!
            }
        }
        2 -> {
            if (mThirdFragment != null) {
                mThirdFragment!!
            } else {
                mThirdFragment = ThirdFragment()
                mThirdFragment!!
            }
        }
        3 -> {
            if (mFourthFragment != null) {
                mFourthFragment!!
            } else {
                mFourthFragment = FourthFragment()
                mFourthFragment!!
            }
        }
        else -> {
            // Else part won't call no need to consider NULL
            mBrowseFragment!!
        }
    }
}
}

FragmentStateAdapter 有多個構造函數選項。 我在這個示例中使用了活動。

FragmentStateAdapter(@NonNull FragmentActivity fragmentActivity)

FragmentStateAdapter(@NonNull Fragment fragment)

FragmentStateAdapter(@NonNull FragmentManager fragmentManager,
        @NonNull Lifecycle lifecycle)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM