最佳做法是将ViewPager2与RecyclerView一起使用,并将Navigvation组件用于RecyclerView的onItemClick

时间:2020-10-27 14:04:29

标签: android android-recyclerview android-architecture-navigation android-viewpager2

我正在从事单一活动,而MVVM artitechture的主要活动只是启动器,然后使用导航组件从此处进入MainFragment,进入ViewPagerContainerFragment,在该片段中,我使用了viewpager2设置tablayout和在viewpager2的每个片段中都有recyclerview,它需要从firebase加载数据,所以一切正常,但是问题是当我在recyclerview中单击一个项目时,这会打开另一个片段以获取详细信息,但是随后我回来,我需要加载所有数据返回,看起来有点连线,有时回收站的视图位置发生了变化,因此,如果从详细片段返回,则有一种方法可以保持viepager2状态,有关详细信息,请参见下面的代码

ViewPager2Adapter.kt

class ViewPager2Adapter(viewPagerContainerFragment : ViewPagerContainerFragment)
: FragmentStateAdapter(viewPagerContainerFragment) {

    override fun getItemCount() = 8

    override fun createFragment(position: Int): Fragment = ItemsViewPagerFragment()

}

ItemsViewPagerFragment.kt

class ItemsViewPagerFragment : Fragment(), ItemsViewPagerFragmentListener{

    private val itemsViewPagerFragmentViewModel: ItemsViewPagerFragmentViewModel by viewModels()

    private lateinit var itemsAdapter: ItemsAdapter

    private val refreshing by lazy { itemsViewPagerFragmentViewModel.refreshing }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.itemsviewpager_fragment, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initSwipeRefreshLayout()
    }

    private fun initSwipeRefreshLayout() {

        refreshing.value = false

        refreshing.observe(viewLifecycleOwner) {
            swipeRefreshLayout.isRefreshing = it
        }

        swipeRefreshLayout.setOnRefreshListener {           
               refreshData()      
        }

    }

    override fun onResume() {
        super.onResume()
        getData()
    }

    private fun getData() {

        if (!::itemsAdapter.isInitialized) {
            refreshing.value = true
            initItemsAdapter()
        }

        showRecyclerView()

    }

    private fun initItemsAdapter() {

        itemsViewPagerFragmentViewModel.getItemsLiveData()

        itemsAdapter = ItemsAdapter(
            emptyList(),
            ItemsClickListener()
        )
    }

    private fun refreshData() {
        if (!::itemsAdapter .isInitialized) initItemsAdapter()
        else itemsViewPagerFragmentViewModel.getItemsLiveData()
    }

    private fun showRecyclerView() {
        if (isAdded) {

            recyclerView.apply {
                setHasFixedSize(true)
                layoutManager =
                    if (DeviceOrientationUtil.isLandscape(requireContext()))
                        StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
                    else LinearLayoutManager(context)

                adapter = itemsAdapter

                viewPagerFragmentViewModel.itemsLiveData.observe(viewLifecycleOwner) {

                        itemsAdapter.updateData(it)

                        refreshing.value = false
                    }

                }

            }

        }
    }

    private inner class ItemsClickListener : ItemsViewHolder.ItemsClickListener {
        override fun onItemClick(item: Item) {
            findNavController().navigate(R.id.action_itemsViewPagerFragment_to_itemDetailFragment)
        }
    }
}

ItemsViewPagerFragmentViewModel.kt

class ItemsViewPagerFragmentViewModel : ViewModel() {

    private val itemsRepository = ItemsRepository()

    val refreshing = MutableLiveData<Boolean>()

    lateinit var itemsLiveData: ItemsLiveData

    var isDataCached = false

    fun getItemsLiveData() {
        itemsLiveData = itemsRepository.getitemsLiveData()
    }

}

0 个答案:

没有答案