Reputation: 800
I'm working with recyclerview
that should use different element positioning(grid for landscape and list for portrait) with different .xml
structure for items.
For the first part I see solution in simple orientation checking in onCreateView()
method:
switch (getResources().getConfiguration().orientation) {
case 1:
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
break;
case 2:
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
break;
}
But for the second part I have doubts. The simplest way is to pass LayoutManager as a constructor parameter to Adapter and check layout in onCreateViewHolder()
with instanceof
operator. But it doesn't look as good
or right solution.
So, are there any common pattern or better way for such situations?
Upvotes: 1
Views: 1852
Reputation: 83577
You should create a folder with the different layouts for each screen size you want to support. See Supporting Multiple Screens for more details. Pay special attention to the suffixes to specify screen size and orientation.
Upvotes: 1
Reputation: 845
To provide different layout resource files depending on screen orientation, you can create resource folders named layout-land (landscape) and layout-port (portrait) and system handles the rest automatically. A tipp: You don't need both folders. If your default orientation is portrait then just create the layout-land folder for landscape mode. The layout resource files have to have the same name!
Upvotes: 5