I added a bottom navigation bar to my activity, after which my RecycleView stopped showing. The log gives an error "RecyclerView: No adapter attached; skipping layout"
As you can see I have attached an adapter for RecyclerView. So why do I keep getting this error?
I have read other questions related to the same problem but none of them help.
Here is my source code:
class Main : AppCompatActivity() {
companion object{
val INTENT_PARCELABLE = "OBJECT_INTENT"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
val foodList = listOf<Food>(
Food(
R.drawable.food1,
"Гамбургер",
"300Г",
"190Р",
"Булка, котлета говяжья, сыр, салат айсберг,\n"
"лук, маринованные огурцы, соус",
),
Food(
R.drawable.food2,
"Шаурма",
"400Г",
"139Р",
"Лаваш, куриное филе, салат, лук, соус\n"
),
Food(
R.drawable.food3,
"Закрытая пицца",
"278Г",
"109Р",
"Фирменная лепешка, соус, куриное филе,\n"
"пекинская капуста, помидоры, сыр,\n"
"соус цезарь"
),
Food(
R.drawable.food4,
"Вок",
"250Г",
"129Р",
"Куриное филе, лапша удон, овощной микс,\n"
"лук, чеснок, имбирь, кунжут, соус"
),
)
val recyclerView = findViewById<RecyclerView>(R.id.rcView)
recyclerView.layoutManager = LinearLayoutManager(this@Main, LinearLayoutManager.HORIZONTAL, false)
recyclerView.adapter = FoodAdapter(this,foodList){
val intent = Intent(this, DetailActivity::class.java)
intent.putExtra(INTENT_PARCELABLE,it)
startActivity(intent)
}
val firstFragment = FirstFragment()
val secondFragment = SecondFragment()
setCurrentFragment(firstFragment)
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.btm_nav)
bottomNavigationView.setOnItemSelectedListener {
when (it.itemId){
R.id.home -> setCurrentFragment(firstFragment)
R.id.card -> setCurrentFragment(secondFragment)
}
true
}
}
fun setCurrentFragment(fragment: Fragment) =
supportFragmentManager.beginTransaction().apply {
replace(R.id.fl_wrapper,fragment)
commit()
}
}
CodePudding user response:
not sure if this is related to your problem but it might help :
if you are using coordinator layout as layout of your xml file you should use layout_behavior att for recycler view set the value to : app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
CodePudding user response:
By using bottomnav, you should use single activity pattern. In your MainActivity, you need to use fragmentcontainer as layout, and after container, put a bottom nav bar, that’s would be better for architecture. Also don’t forget to create nav_graph file into nav folder in your res