android-fragments 如何在Tablayout中的不同片段之间切换时保存Webview状态?

3wabscal  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(180)

我正在制作一个非常简单的webview应用程序,它可以将网站的不同子页面放在不同的标签下(使用tablayout),例如消息、订单等。使用片段创建单独的网页视图。其理念是用户可以在页面之间快速切换。在测试时,当我切换到我所在标签旁边的标签,然后再切换回来时,页面与我离开该选项卡时的页面完全相同。但是,如果我切换到与当前选项卡相距不止一个选项卡的选项卡,然后切换回上一个选项卡,则页面将重新加载。我希望在用户切换到任何其他选项卡时记住页面状态。如何实现这一点?
TL;DR:如何在使用tablayout(每个片段显示一个不同的web视图)在选项卡之间切换时保持web视图状态?
以下是其中一个片段的代码,以供参考:

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
}

}
由www.example.com控制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
}

}

hkmswyz6

hkmswyz61#

不推荐使用寻呼机适配器

使用检视分页器2

首先将offscreenPageLimit设置为您的viewPager2

//YOUR_ID
  yourViewPager2.offscreenPageLimit =3

使用页面状态适配器

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有多个构造函数选项。我在这个示例中使用了Activity。

FragmentStateAdapter(@NonNull FragmentActivity fragmentActivity)

FragmentStateAdapter(@NonNull Fragment fragment)

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

相关问题