2

When i use paging 3 submitData with viewpager2 ,View flickers. But when i change ViewPager + FragmentStatePagerAdapter replace ViewPager2 + FragmentStateAdapter,it works well. This is my code:

class ViewPagerAdapter(
    fragment: Fragment,
    private val fragments: List<Fragment>
) : FragmentStateAdapter(fragment) {
    override fun getItemCount(): Int = fragments.size

    override fun createFragment(position: Int): Fragment = fragments[position]
}

@HiltViewModel
class HomeViewModel @Inject constructor(
    private val wanConfigRepository: WanConfigRepository,
    private val wanArticleRepository: WanArticleRepository
) : ViewModel() {
    val bannerLiveData: MutableLiveData<List<BannerModel>> = MutableLiveData()

    val flow = wanArticleRepository.getArticleListFlow().cachedIn(viewModelScope)

}

class WanArticlePagingSource(
    private val apiWanArticleServer: ApiWanArticleServer
) : PagingSource<Int, ArticleModel>() {
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ArticleModel> {
        return try {
            val nextPageNumber = params.key ?: 0
            val response = apiWanArticleServer.listArticle(nextPageNumber)
            val curPage = response.data?.curPage ?: 1
            LoadResult.Page(
                data = response.data?.datas ?: listOf(),
                prevKey = if (curPage <= 1) null else curPage - 2,
                nextKey = if (curPage >= response.data?.pageCount ?: 0) null else curPage
            )
        } catch (e: Exception) {
            LoadResult.Error(e)
        }
    }

    override fun getRefreshKey(state: PagingState<Int, ArticleModel>): Int? = state.anchorPosition
}

//in Fragment
lifecycleScope.launch {
    homeViewModel.flow.collect { pagingData ->
        articleHomeAdapter.submitData(pagingData)
    }
}
//in Activity
 TabLayoutMediator(binding.tabLayout, binding.viewPager, true, false) { tab, position ->
            val itemTabMainBinding =
                ItemTabMainBinding.inflate(LayoutInflater.from(binding.tabLayout.context))
            itemTabMainBinding.icon.setImageResource(tabDrawable[position])
            itemTabMainBinding.icon.setColorFilter(
                ContextCompat.getColor(
                    itemTabMainBinding.icon.context,
                    R.color.gray_alpha
                )
            )
            itemTabMainBinding.name.text = tabTexts[position]
            itemTabMainBinding.name.setTextColor(
                ContextCompat.getColor(
                    itemTabMainBinding.name.context,
                    R.color.gray_alpha
                )
            )
            tab.customView = itemTabMainBinding.root
        }.attach()

But when i change ViewPager + FragmentStatePagerAdapter replace ViewPager2 + FragmentStateAdapter,it works well.

Heven Holt
  • 21
  • 3

1 Answers1

0

I found the cause of the problem,When using paging+ViewPager2+ConstraintLayout at the same time,The view will flicker. This is the previous code:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/tab_layout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/tab_wan_home_bottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:tabIndicatorHeight="0dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

This is the corrected code:

<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/tab_wan_home_bottom"
        app:tabIndicatorHeight="0dp" />

</androidx.appcompat.widget.LinearLayoutCompat>

I don't know what caused it, it looks like the height of ConstraintLayout is set to zero?

Heven Holt
  • 21
  • 3