Skip to content

Instantly share code, notes, and snippets.

@RowlandOti
Created January 1, 2016 15:04
Show Gist options
  • Save RowlandOti/3cafcfe0761b445865a1 to your computer and use it in GitHub Desktop.
Save RowlandOti/3cafcfe0761b445865a1 to your computer and use it in GitHub Desktop.
public class MovieFragemnt {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Create new instance of layout manager
final StaggeredGridLayoutManager mStaggeredLayoutManger = new StaggeredGridLayoutManager(getNumberOfColumns(), StaggeredGridLayoutManager.VERTICAL);
// Set the layout manger
mMovieRecycleView.setLayoutManager(mStaggeredLayoutManger);
mMovieRecycleView.setHasFixedSize(false);
// Call is actually only necessary with custom ItemAnimators
mMovieRecycleView.setItemAnimator(new DefaultItemAnimator());
// Create new adapter
mMovieAdapter = new MovieAdapter(mMovieList, getContext(), getActivity());
// Associate RecycleView with adapter
mMovieRecycleView.setAdapter(mMovieAdapter);
}
// Get the no. of grid columns to use
protected int getNumberOfColumns() {
// The number of grid columns
int numberColumns = 2;
// Check if we are in landscape
if (ScreenUtility.isInLandscapeOrientation(getContext())) {
numberColumns = 3;
}
// Return the no. of columns
return numberColumns;
}
}
/**
* Provides utility methods for working with the device screen.
*/
public class ScreenUtility {
/**
* @param context Context instance
*
* @return [true] if the device is in landscape orientation, [false] otherwise.
*/
public static boolean isInLandscapeOrientation(Context context) {
Configuration configuration = context.getResources().getConfiguration();
return configuration.orientation == Configuration.ORIENTATION_LANDSCAPE;
}
}
@RowlandOti
Copy link
Author

An example of how to dynamically assign no. of grid columns in RecycleView

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment