Reputation: 47
RecyclerView does not switch from two columns to three columns when I rotate the view to landscape. I tried this code:
mGLManager.setSpanCount(3);
mGLManager = (GridLayoutManager)mRecyclerView.getLayoutManager();
mGLManager.setSpanCount(2);
mRecyclerView.setLayoutManager(mGLManager);
mRecyclerView.setLayoutManager( new GridLayoutManager(MainActivity.this, 3) );
I also tried these lines to get it to refresh:
mRecyclerView.refreshDrawableState();
mAdapter.notifyDataSetChanged();
mRecyclerView.invalidate();
mRecyclerView.requestLayout();
mRecyclerView.swapAdapter(mRecyclerView.getAdapter(), true);
mRecyclerView.scrollBy(0,0);
Here is my current code.
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// set the number of columns based on the orientation
if( newConfig.orientation == Configuration.ORIENTATION_PORTRAIT ) {
mRecyclerView.setLayoutManager( new GridLayoutManager(MainActivity.this, 2));
mRecyclerView.invalidate();
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
} else if( newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ) {
mRecyclerView.setLayoutManager( new GridLayoutManager(MainActivity.this, 3));
mRecyclerView.invalidate();
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
}
}
The toasts demonstrate that the code runs when I flip the orientation. Thanks for your help!
Upvotes: 1
Views: 1311
Reputation: 13358
Add inside your onCreateView method of Activity it will be called each time of an orientation change
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
mRecycler.setLayoutManager(new GridLayoutManager(MainActivity.this, 2));
}
else{
mRecycler.setLayoutManager(new GridLayoutManager(MainActivity.this, 3));
}
Upvotes: 5