在我的应用程序中,我正在尝试使用ViewPager
创建2个片段。这些片段有3个标签/页面,每个标签都有RecyclerView
个,它们应该由数据库支持,因此我将它们保存在List
中。
class MainActivity : AppCompatActivity() {
val fragments = listOf(SwipeFragment(), SwipeFragment())
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportFragmentManager.beginTransaction().replace(R.id.placeholder, fragments[0]).commit()
button1.setOnClickListener {
supportFragmentManager.beginTransaction().replace(R.id.placeholder, fragments[0]).commit()
}
button2.setOnClickListener {
supportFragmentManager.beginTransaction().replace(R.id.placeholder, fragments[1]).commit()
}
}
}
问题是,在离开SwipeFragment
并返回后,视图似乎是空的。然后,如果您导航到例如最左边的页面,最右边的一个“出现”(当你去它)。
这导致中间页面保持空白。 (原因是FragmentStatePagerAdapter
默认只保留当前页面和2个相邻的页面。中间的一个不会在带有3个标签的布局中刷新 - 我尝试了5个标签而且我是能够通过往返来带回所有人。
经过一些调试后,我发现表示页面的片段不会从FragmentManager
中删除,但主SwipeFragment
是。{即使在阅读了几乎所有的源代码之后,我也无法理解FragmentManager
如何真正起作用。
如果我能够以某种方式安全地从FragmentManager
中删除碎片,它可能会起作用
我已经尝试了一些保存状态的技术,但框架会自动执行(实际上可能是导致此问题的原因)
我只是Android的初学者,所以无论如何都可能有更好的方法。我将把剩下的文件留在这里作为参考。
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.test.MainActivity">
<FrameLayout
android:id="@+id/placeholder"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
style="?android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button1" />
<Button
android:id="@+id/button2"
style="?android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button2" />
</LinearLayout>
</LinearLayout>
SwipeFragment.kt
class SwipeFragment : Fragment() {
// TODO: set up and keep the database here
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.fragment_swipe, container, false)
view.tab_layout.setupWithViewPager(view.pager)
view.pager.adapter = DummyPagerAdapter(activity.supportFragmentManager, index)
return view
}
}
fragment_swipe.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.SwipeFragment">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.view.ViewPager>
ItemListFragment.kt
class ItemListFragment() : Fragment() {
// Or keep the database here?
// Probably not the best idea though
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.fragment_item_list, container, false)
if (view is RecyclerView) {
view.layoutManager = LinearLayoutManager(view.context)
view.adapter = MyItemRecyclerViewAdapter(DummyContent.ITEMS)
}
return view
}
}
DummyPagerAdapter.kt
class DummyPagerAdapter(manager: FragmentManager, val parentIndex: Int) :
FragmentStatePagerAdapter(manager) {
override fun getItem(position: Int): Fragment = ItemListFragment()
override fun getPageTitle(position: Int): CharSequence = "${ position + 1 }"
override fun getCount(): Int = 3
}
Android Studio生成的RecyclerView.Adapter
的基本实现
MyItemRecyclerViewAdapter.kt
class MyItemRecyclerViewAdapter(val values: List<DummyItem>) :
RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder>() {
...
}
fragment_item_list.xml
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:name="com.example.test.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
tools:context="com.example.test.ItemListFragment"
tools:listitem="@layout/fragment_item" />
答案 0 :(得分:5)
您应该在def _read32(bytestream):
File "<stdin>", line 11
def _read32(bytestream):
^
SyntaxError: invalid syntax
内使用childFragmentManager
代替activity.supportFragmentManager
。
SwipeFragment
view.pager.adapter = DummyPagerAdapter(childFragmentManager, index)
和getSupportFragmentManager()
之间的差异已经讨论here。
基本上,区别在于Fragment现在有自己的内部FragmentManager可以处理Fragments。子FragmentManager是处理仅包含在它所添加的Fragment中的Fragments的子类。另一个FragmentManager包含在整个Activity中。
答案 1 :(得分:0)
在我的情况下,childFragmentManager
没有帮助。我认为我们可以使用模式“观察者”从另一个片段返回时用新数据更新ViewPager
的现有片段。