[英]Remove and Add new Tab in ViewPager with Fragment WebView
I am trying to create a browser for Android, so for that, I thought using viewpager
with FragmentStatePagerAdapter
it's a good idea.我正在尝试为 Android 创建一个浏览器,因此,我认为将viewpager
与FragmentStatePagerAdapter
一起使用是个好主意。 But once I removed one tab and adding a new one bringing back the old fragment, but I want a new tab with given URL.但是,一旦我删除了一个标签并添加了一个新标签,将旧片段带回来,但我想要一个带有给定 URL 的新标签。
PageAdapter.kt页面适配器.kt
class PageAdapter(
private val context: Context,
private val fragmentManager: FragmentManager,
private val tabs: ArrayList<Tab>
): FragmentStatePagerAdapter(fragmentManager) {
companion object {
val TAG = PageAdapter::class.java.simpleName
}
override fun getCount(): Int {
return tabs.size
}
override fun getItem(position: Int): Fragment {
return tabs[position].fragment
}
override fun getPageTitle(position: Int): CharSequence? {
return "View " + (position + 1)
}
}
newTab()新标签()
fun newTab(url: String) = runBlocking {
val id = UUID.randomUUID().toString()
val bundle = Bundle()
bundle.putString("id", id)
bundle.putString("url", url)
val fragment = WebViewFragment()
fragment.arguments = bundle
val tab = Tab(id, url, url, fragment)
tabs.add(tab)
currentTabIndex = tabs.size - 1
launch(Dispatchers.IO) {
database?.eagleDao()?.putId(TabId(id))
}
}
removeTab()删除标签()
fun removeTab(id: String) = runBlocking {
findTabPosition(id)?.also {
val tab = tabs[it]
launch(Dispatchers.IO) {
database?.eagleDao()?.removeId(tab.id)
}
tabs.removeAt(it)
}
currentTabIndex = tabs.lastIndex
}
According to the official documentation FragmentStatePagerAdapter
is only a simple list view and it's behaviour is thus correct: to "save resources" no background information is saved when you close it.根据官方文档FragmentStatePagerAdapter
只是一个简单的列表视图,因此它的行为是正确的:为了“保存资源”,关闭它时不会保存背景信息。
Why don't you stay with webview
instead and try to accomplish the tabbed view there?为什么不继续使用webview
并尝试在那里完成选项卡式视图? I think as as you work with URLs and pretend you work on a browser (web browser, I suppose), maybe this entry might be of help: How to make tabs with webview to load from a web address?我认为当你使用 URL 并假装你在浏览器上工作时(我想是网络浏览器),也许这个条目可能会有所帮助: 如何使用 webview 制作标签以从网址加载?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.