Junior — Middle
How to implement changing the appearance of RecyclerView depending on the device's screen orientation?
sobes.tech AI
Answer from AI
To change the appearance of RecyclerView depending on the screen orientation, you can use different LayoutManagers or different XML layouts.
Example implementation in an Activity or Fragment:
val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// In landscape orientation, use GridLayout with 2 columns
recyclerView.layoutManager = GridLayoutManager(this, 2)
} else {
// In portrait orientation, use LinearLayout vertically
recyclerView.layoutManager = LinearLayoutManager(this)
}
recyclerView.adapter = myAdapter
You can also define different resources for different orientations, such as different layout files or resource values, to adapt the appearance of elements.