Class DynamicLayoutUtils
java.lang.Object
com.pranavpandey.android.dynamic.support.util.DynamicLayoutUtils
Helper class to perform layout operations like detecting the column count at runtime.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Default column count for the collection views. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic com.google.android.flexbox.FlexboxLayoutManager
getFlexboxLayoutManager
(Context context, int flexDirection) Returns theFlexboxLayoutManager
object for the given context.static com.google.android.flexbox.FlexboxLayoutManager
getFlexboxLayoutManager
(Context context, int flexDirection, int justifyContent, int alignItems) Returns theFlexboxLayoutManager
object for the given context.static int
getGridCount
(Context context) Get the grid count for the supplied context.static int
getGridCountCompact
(Context context) Get the grid count for the supplied context suitable for a compact layout.static int
getGridCountCompactDialog
(Context context) Get the grid count for the supplied context suitable for the dialog.static int
getGridCountNoDrawer
(Context context) Get the grid count for the supplied context suitable for no drawer activity.static GridLayoutManager
getGridLayoutManager
(Context context, int count, int orientation) Returns theGridLayoutManager
object for the given context.static int
getLayoutColumns
(Context context, int defaultCount, int maxCount, boolean compact) Get the column count according to the current configuration.static LinearLayoutManager
getLinearLayoutManager
(Context context, int orientation) Returns theLinearLayoutManager
object for the given context.static int
getScreenSizeCategory
(Context context) Get the screen size category for the supplied context.static StaggeredGridLayoutManager
getStaggeredGridLayoutManager
(int count, int orientation) Returns theStaggeredGridLayoutManager
object for the given context.static boolean
isInMultiWindowMode
(Context context) Checks whether the app is in multi-window mode.static void
setFullSpanForPosition
(RecyclerView recyclerView) Sets full span for the single last position in case of aGridLayoutManager
.static void
setFullSpanForPosition
(RecyclerView recyclerView, Integer[] positions) Sets full span for the positions in case of aGridLayoutManager
.static void
setFullSpanForPosition
(RecyclerView recyclerView, Integer[] positions, int spanCount) Sets full span for the positions in case of aGridLayoutManager
.static void
setFullSpanForType
(RecyclerView recyclerView) Sets full span for the header and empty view in case of aGridLayoutManager
.static void
setFullSpanForType
(RecyclerView recyclerView, Integer[] itemTypes, int spanCount) Sets full span for the item view types in case of aGridLayoutManager
.static void
setFullSpanForView
(View view) Sets full span for the view in case of aStaggeredGridLayoutManager
.
-
Field Details
-
SPAN_DEFAULT
public static final int SPAN_DEFAULTDefault column count for the collection views.- See Also:
-
-
Constructor Details
-
DynamicLayoutUtils
public DynamicLayoutUtils()
-
-
Method Details
-
getLayoutColumns
public static int getLayoutColumns(@NonNull Context context, int defaultCount, int maxCount, boolean compact) Get the column count according to the current configuration.It will also consider multi-window mode on API 24 and above.
It is not recommended to do this calculation at runtime. So, please define all the span counts in xml.
- Parameters:
context
- The context to get configuration.defaultCount
- The default column count.maxCount
- The maximum column count up to which we can scale.compact
-true
if the layout is compact and disable the auto increase of columns in multi-window mode.- Returns:
- The column count according to the current device configurations.
-
isInMultiWindowMode
Checks whether the app is in multi-window mode.- Parameters:
context
- The context to get configuration.- Returns:
true
if the app is in multi-window mode.
-
getScreenSizeCategory
Get the screen size category for the supplied context.- Parameters:
context
- The context to get configuration.- Returns:
- The screen size category for the supplied context.
-
getGridCount
Get the grid count for the supplied context.- Parameters:
context
- The context to get the span count.- Returns:
- The grid count for the supplied context with default count 1 and max count 2.
-
getGridCountNoDrawer
Get the grid count for the supplied context suitable for no drawer activity.- Parameters:
context
- The context to get the span count.- Returns:
- The grid count for the supplied context with default count 1 and max count 3.
-
getGridCountCompact
Get the grid count for the supplied context suitable for a compact layout.- Parameters:
context
- The context to get the span count.- Returns:
- The grid count for the supplied context with default count 1 or 2 and max count 3.
-
getGridCountCompactDialog
Get the grid count for the supplied context suitable for the dialog.- Parameters:
context
- The context to get the span count.- Returns:
- The grid count for the supplied context with default count 1 or 2 and max count 3.
-
getLinearLayoutManager
@NonNull public static LinearLayoutManager getLinearLayoutManager(@NonNull Context context, int orientation) Returns theLinearLayoutManager
object for the given context.- Parameters:
context
- The context to instantiate the layout manager.orientation
- The orientation of the layout manager.LinearLayoutManager.VERTICAL
orLinearLayoutManager.HORIZONTAL
- Returns:
- The
LinearLayoutManager
object for the given context.
-
getGridLayoutManager
@NonNull public static GridLayoutManager getGridLayoutManager(@NonNull Context context, int count, int orientation) Returns theGridLayoutManager
object for the given context.- Parameters:
context
- The context to instantiate the layout manager.count
- The column count for the grid layout.orientation
- The orientation of the layout manager.LinearLayoutManager.VERTICAL
orLinearLayoutManager.HORIZONTAL
- Returns:
- The
GridLayoutManager
object for the given context.
-
getStaggeredGridLayoutManager
@NonNull public static StaggeredGridLayoutManager getStaggeredGridLayoutManager(int count, int orientation) Returns theStaggeredGridLayoutManager
object for the given context.- Parameters:
count
- The no. of rows or columns count according to the orientation.orientation
- The orientation of the layout manager.StaggeredGridLayoutManager.VERTICAL
orStaggeredGridLayoutManager.HORIZONTAL
- Returns:
- The
StaggeredGridLayoutManager
object for the given context.
-
getFlexboxLayoutManager
@NonNull public static com.google.android.flexbox.FlexboxLayoutManager getFlexboxLayoutManager(@NonNull Context context, int flexDirection, int justifyContent, int alignItems) Returns theFlexboxLayoutManager
object for the given context.- Parameters:
context
- The context to instantiate the layout manager.flexDirection
- The flex direction attribute to the flex container.justifyContent
- The justify content attribute to the flex container.alignItems
- The align items attribute to the flex container.- Returns:
- The
FlexboxLayoutManager
object for the given context.
-
getFlexboxLayoutManager
@NonNull public static com.google.android.flexbox.FlexboxLayoutManager getFlexboxLayoutManager(@NonNull Context context, int flexDirection) Returns theFlexboxLayoutManager
object for the given context.- Parameters:
context
- The context to instantiate the layout manager.flexDirection
- The flex direction attribute to the flex container.- Returns:
- The
FlexboxLayoutManager
object for the given context.
-
setFullSpanForType
public static void setFullSpanForType(@Nullable RecyclerView recyclerView, @NonNull Integer[] itemTypes, int spanCount) Sets full span for the item view types in case of aGridLayoutManager
. This method must be called after setting an adapter for the recycler view.- Parameters:
recyclerView
- The recycler view to set the span size.itemTypes
- The item types supported by the recycler view.spanCount
- The maximum span supported by the layout manager.- See Also:
-
setFullSpanForType
Sets full span for the header and empty view in case of aGridLayoutManager
. This method must be called after setting an adapter for the recycler view.- Parameters:
recyclerView
- The recycler view to set the span size.- See Also:
-
setFullSpanForPosition
public static void setFullSpanForPosition(@Nullable RecyclerView recyclerView, @NonNull Integer[] positions, int spanCount) Sets full span for the positions in case of aGridLayoutManager
. This method must be called after setting an adapter for the recycler view.- Parameters:
recyclerView
- The recycler view to set the span size.positions
- The positions supported by the recycler view.spanCount
- The maximum span supported by the layout manager.- See Also:
-
setFullSpanForPosition
public static void setFullSpanForPosition(@Nullable RecyclerView recyclerView, @NonNull Integer[] positions) Sets full span for the positions in case of aGridLayoutManager
. This method must be called after setting an adapter for the recycler view.- Parameters:
recyclerView
- The recycler view to set the span size.positions
- The positions supported by the recycler view.- See Also:
-
setFullSpanForPosition
Sets full span for the single last position in case of aGridLayoutManager
. This method must be called after setting an adapter for the recycler view.- Parameters:
recyclerView
- The recycler view to set the span size.- See Also:
-
setFullSpanForView
Sets full span for the view in case of aStaggeredGridLayoutManager
. This method must be called after setting an adapter for the recycler view.- Parameters:
view
- The recycler view to set the span size.- See Also:
-