skip to Main Content

It loads fine at startup but when I navigate to other fragments and go back to Home Fragment, the recycler view doesn’t load anymore. It loads fine with other fragments when I navigate back to it, only the recycler view of Home Fragment doesn’t show. There is no error showing in the logcat. Only this W/RecyclerView: No adapter attached; skipping layout. Anyone can help me? I don’t know what’s really wrong here. Thank you in advance guys.

Here is my code of HOME FRAGMENT

class HomeFragment : Fragment() {

private lateinit var homeViewModel: HomeViewModel

var recyclerView:RecyclerView?=null
lateinit var swipeUpToRefresh:SwipeRefreshLayout
private var viewPager:LoopingViewPager?=null

private var layoutAnimationController:LayoutAnimationController?=null

private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    homeViewModel =
        ViewModelProvider(this).get(HomeViewModel::class.java)
    _binding = FragmentHomeBinding.inflate(inflater, container, false)
    val root: View = binding.root

    val key = requireArguments().getString("restaurant")

    initView(root)
    refreshPage()
    //bind data
        homeViewModel.getPopularList(key!!).observe(viewLifecycleOwner, {
            val listData = it
            val adapter = MyPopularCategoriesAdapter(requireContext(), listData)
            recyclerView!!.adapter = adapter
            recyclerView!!.layoutAnimation = layoutAnimationController
        })
        homeViewModel.getBestDealList(key).observe(viewLifecycleOwner, {
            val adapter = MyBestDealsAdapter(requireContext(), it, false)
            viewPager!!.adapter = adapter
        })
    return root
}

private fun refreshPage() {
    val key = requireArguments().getString("restaurant")
    swipeUpToRefresh.setOnRefreshListener {
        //bind data
        homeViewModel.getPopularList(key!!).observe(viewLifecycleOwner, {
            val listData = it
            val adapter = MyPopularCategoriesAdapter(requireContext(), listData)
            recyclerView!!.adapter = adapter
            recyclerView!!.layoutAnimation = layoutAnimationController
        })
        homeViewModel.getBestDealList(key).observe(viewLifecycleOwner, {
            val adapter = MyBestDealsAdapter(requireContext(), it, false)
            viewPager!!.adapter = adapter
        })
        swipeUpToRefresh.isRefreshing = false
    }
}

private fun initView(root:View) {
    swipeUpToRefresh = root.findViewById(R.id.refresh) as SwipeRefreshLayout

    layoutAnimationController = AnimationUtils.loadLayoutAnimation(context, R.anim.layout_item_from_left)

    viewPager = root.findViewById(R.id.viewpager) as LoopingViewPager

    recyclerView = root.findViewById(R.id.recycler_popular) as RecyclerView
    recyclerView!!.setHasFixedSize(true)
    recyclerView!!.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
}

override fun onResume() {
    super.onResume()
    viewPager!!.resumeAutoScroll()
}

override fun onPause() {
    viewPager!!.pauseAutoScroll()
    super.onPause()
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}}

2

Answers


  1. Chosen as BEST ANSWER

    I added this line of code to my HomeActivity in R.id.nav_home

    val bundle = Bundle()
    bundle.putString("restaurant",Common.currentRestaurant!!.uid)
                        navController.navigate(R.id.nav_home,bundle)
    

    here Is my navView

    navView!!.setNavigationItemSelectedListener { p0 ->
                p0.isChecked = true
                drawer!!.closeDrawers()
    
                if (p0.itemId == R.id.nav_sign_out) {
                    signOut()
                } else if (p0.itemId == R.id.nav_restaurant) {
                    if (menuItemClick != p0.itemId)
                        navController.popBackStack()
                    navController.navigate(R.id.nav_restaurant)
                } else if (p0.itemId == R.id.nav_home) {
                    if (menuItemClick != p0.itemId) {
                        EventBus.getDefault().postSticky(MenuInflateEvent(true))
                        navController.popBackStack()
                        val bundle = Bundle()
                        bundle.putString("restaurant",Common.currentRestaurant!!.uid)
                        navController.navigate(R.id.nav_home,bundle)
                    }
                } else if (p0.itemId == R.id.nav_cart) {
                    if (menuItemClick != p0.itemId) {
                        EventBus.getDefault().postSticky(MenuInflateEvent(true))
                        navController.popBackStack()
                        navController.navigate(R.id.nav_cart)
                    }
                } else if (p0.itemId == R.id.nav_menu) {
                    if (menuItemClick != p0.itemId) {
                        EventBus.getDefault().postSticky(MenuInflateEvent(true))
                        navController.popBackStack()
                        navController.navigate(R.id.nav_menu)
                    }
                } else if (p0.itemId == R.id.nav_view_order) {
                    if (menuItemClick != p0.itemId) {
                        EventBus.getDefault().postSticky(MenuInflateEvent(true))
                        navController.popBackStack()
                        navController.navigate(R.id.nav_view_order)
                    }
                } else if (p0.itemId == R.id.nav_update_info) {
                    showUpdateDialog()
                }
    
                menuItemClick = p0.itemId
    
                true
            }
    

  2. Try doing this

    private fun initView(root:View) {
        swipeUpToRefresh = root.findViewById(R.id.refresh) as SwipeRefreshLayout
    
        layoutAnimationController = AnimationUtils.loadLayoutAnimation(context, R.anim.layout_item_from_left)
    
        viewPager = root.findViewById(R.id.viewpager) as LoopingViewPager
       
        recyclerView = root.findViewById(R.id.recycler_popular) as RecyclerView
        //Here is where you need to initialize an adapter to the recyclerView
        recylerView!!.adapter = MyPopularCategoriesAdapter(requireContext(), listData) //I'm not sure what is the second argument of your adapter. Feel free to change it as you like.
        recyclerView!!.setHasFixedSize(true)
        recyclerView!!.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search