在TableOut中的不同片段之间切换时,如何保存WebView状态?

发布于 2025-02-14 01:08:32 字数 1670 浏览 0 评论 0原文

我正在制作一个非常简单的WebView应用程序,该应用程序将网站的不同子页面放在不同的选项卡下(使用TableAut),IE消息,订单等。单个WebViews是使用Fragments创建的。想法是用户可以在页面之间快速来回切换。在测试时,当我直接切换到我在上面的标签旁边,然后返回时,该页面正是我离开标签时的情况。但是,我切换到一个比目前的选项卡更远的选项卡,然后切换回上一个选项卡,该页面已重新加载。我希望当用户切换到任何其他选项卡时,请记住页面状态。怎么办?

TL; DR:如何使用TableAut使用TableOut和每个显示不同的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
}

}

I am making a very simple webview app that places different subpages of a website under different tabs (using tablayout), i.e. messages, orders etc. The individual webviews are created using fragments. The idea is that the user can quickly switch back and forth between pages. While testing, when I switch to the tab directly next to the one I'm on and then back, the page is exactly how it was when I left the tab. However, I switch to a tab that is further than one tab away from the one I'm currently on, and then switch back to the previous tab, the page is reloaded. I want the page state to be remembered when the user switches to any other tab. How can this be done?

TL;DR: how to retain webview states when switching between tabs using tablayout with fragments that each display a different webview?

For reference, the code of one of the fragments:

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
}

}

Controlled by a 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
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰之心 2025-02-21 01:08:32

PAGER适配器弃用

使用ViewPager2

首先将屏幕上的PAGELIMIT设置为您的ViewPager2

//YOUR_ID
  yourViewPager2.offscreenPageLimit =3

使用PagerStateDapter

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!!
        }
    }
}
}

FragmentStatapater具有多个构造函数选项。我在此样本中使用了活动。

FragmentStateAdapter(@NonNull FragmentActivity fragmentActivity)

FragmentStateAdapter(@NonNull Fragment fragment)

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

Pager Adapter Deprecated

Use ViewPager2

First set offscreenPageLimit to your viewPager2

//YOUR_ID
  yourViewPager2.offscreenPageLimit =3

Use 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 have multiple constructor options. I used activity in this sample.

FragmentStateAdapter(@NonNull FragmentActivity fragmentActivity)

FragmentStateAdapter(@NonNull Fragment fragment)

FragmentStateAdapter(@NonNull FragmentManager fragmentManager,
        @NonNull Lifecycle lifecycle)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文