【发布时间】:2018-09-09 03:08:26
【问题描述】:
我目前正在使用ViewPager 和TabLayout 来处理几个类似的片段。 ViewPager 中托管的片段都有一个RecyclerView。当我滑动超过一页时,片段(我认为?)被破坏。当我向后滑动时,它会重新创建。
经过调试,我发现 Fragment 中的适配器不为空,并且填充了之前的数据。但是,一旦片段可见,它就不再显示任何条目。
这是 ViewPager 中的一个片段
class ArtistListFragment : Fragment(), ListView {
override val title: String
get() = "Artists"
private val artistList: RecyclerView by lazy { artist_list }
@Inject lateinit var api: SaddleAPIManager
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
= container?.inflate(R.layout.fragment_list_artist)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
artistList.apply {
setHasFixedSize(true)
if (adapter == null) {
adapter = ViewTypeAdapter(mapOf(AdapterConstants.ARTIST to ArtistDelegateAdapter()))
}
layoutManager = LinearLayoutManager(context)
if (savedInstanceState != null && savedInstanceState.containsKey("artist")) {
(adapter as ViewTypeAdapter).setItems(savedInstanceState.getParcelableArrayList<Artist>("artists"))
}
}
(activity?.application as SaddleApplication).apiComponent.inject(this)
refreshData()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelableArrayList(
"artists",
(artistList.adapter as ViewTypeAdapter).items as ArrayList<out Parcelable>
)
}
private fun refreshData() = launch(UI) {
val result = withContext(CommonPool) { api.getArtists() }
when(result) {
is Success -> (artistList.adapter as ViewTypeAdapter).setItems(result.data.results)
is Failure -> Snackbar.make(artistList, result.error.message ?: "Error", Snackbar.LENGTH_LONG).show()
}
}
}
这是托管 ViewPager 的片段
class NavigationFragment : Fragment() {
private val viewPager: ViewPager by lazy { pager }
private val tabLayout: TabLayout by lazy { tab_layout }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
= container?.inflate(R.layout.fragment_navigation)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewPager.apply {
adapter = NavigationPageAdapter(childFragmentManager)
}
tabLayout.apply {
setupWithViewPager(viewPager)
}
}
}
我用于分页的适配器
class NavigationPageAdapter(fragmentManager: FragmentManager) : FragmentStatePagerAdapter(fragmentManager) {
companion object {
const val NUM_PAGES = 4
}
private val pages: List<Fragment> = (0 until NUM_PAGES).map {
when (it) {
0 -> ArtistListFragment()
1 -> AlbumListFragment()
2 -> TrackListFragment()
else -> PlaylistListFragment()
} as Fragment
}
override fun getItem(position: Int): Fragment = pages[position]
override fun getCount(): Int = NUM_PAGES
override fun getPageTitle(position: Int): CharSequence? = (pages[position] as ListView).title
}
我已尝试覆盖 onSaveInstanceState 并从包中读取信息。它似乎没有做任何事情。问题似乎实际上是 RecyclerView 显示?它充满了数据,这就是我难过的原因。
【问题讨论】:
-
你有没有尝试使用Android架构的ViewModel。
-
不,但我的下一个任务是在我弄清楚这一点后实施 MVVM。最终这并不能解决问题,因为我不想每次重新创建片段时都访问 API。
-
您不会每次都使用 ViewModel 访问 API。 ViewModel 可以让您在配置更改后幸免于难。所以这可能会奏效.. IMO
-
使用 LiveData 实现 ViewModel 并没有解决问题。
标签: android android-fragments android-recyclerview kotlin android-viewpager