Created
January 1, 2016 15:04
-
-
Save RowlandOti/3cafcfe0761b445865a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example of how to dynamically assign no. of grid columns in RecycleView