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
I added this line of code to my HomeActivity in R.id.nav_home
here Is my navView
Try doing this