Skip to content

Commit

Permalink
Android: Dynamically adapt grid span to card_game size
Browse files Browse the repository at this point in the history
In order to avoid getting stuck making a new dimension file every time a new device is found we take a known value for how large the game card will be, take the screen size, and adjust the grid accordingly.
  • Loading branch information
t895 committed Nov 13, 2022
1 parent f614f94 commit eb060c7
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 45 deletions.
Expand Up @@ -6,12 +6,12 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.material.color.MaterialColors;
Expand Down Expand Up @@ -62,11 +62,33 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
public void onViewCreated(@NonNull View view, Bundle savedInstanceState)
{
mSwipeRefresh = mBinding.swipeRefresh;

int columns = getResources().getInteger(R.integer.game_grid_columns);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), columns);
mAdapter = new GameAdapter(requireActivity());

// Here we have to make sure the fragment is attached to an activity, wait for the layout
// to be drawn, and make sure it is drawn with a width > 0 before finding the correct
// span for our grid layout. Once drawn correctly, we can stop listening for layout changes.
if (isAdded())
{
view.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
int columns = mBinding.getRoot().getMeasuredWidth() /
requireContext().getResources().getDimensionPixelSize(R.dimen.card_width);
if (columns == 0)
{
columns = 1;
}
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), columns);
mBinding.gridGames.setLayoutManager(layoutManager);
mBinding.gridGames.setAdapter(mAdapter);
}
});
}

// Set theme color to the refresh animation's background
mSwipeRefresh.setProgressBackgroundColorSchemeColor(
MaterialColors.getColor(mSwipeRefresh, R.attr.colorPrimary));
Expand All @@ -75,9 +97,6 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState)

mSwipeRefresh.setOnRefreshListener(mOnRefreshListener);

mBinding.gridGames.setLayoutManager(layoutManager);
mBinding.gridGames.setAdapter(mAdapter);

InsetsHelper.setUpList(getContext(), mBinding.gridGames);

setRefreshing(GameFileCacheManager.isLoadingOrRescanning());
Expand Down
4 changes: 1 addition & 3 deletions Source/Android/app/src/main/res/layout/fragment_grid.xml
Expand Up @@ -7,9 +7,7 @@
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin">
android:layout_height="wrap_content">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/grid_games"
Expand Down
4 changes: 0 additions & 4 deletions Source/Android/app/src/main/res/values-w1000dp/integers.xml

This file was deleted.

6 changes: 0 additions & 6 deletions Source/Android/app/src/main/res/values-w1050dp/dimens.xml

This file was deleted.

4 changes: 0 additions & 4 deletions Source/Android/app/src/main/res/values-w300dp/integers.xml

This file was deleted.

4 changes: 0 additions & 4 deletions Source/Android/app/src/main/res/values-w400dp/integers.xml

This file was deleted.

4 changes: 0 additions & 4 deletions Source/Android/app/src/main/res/values-w500dp/integers.xml

This file was deleted.

4 changes: 0 additions & 4 deletions Source/Android/app/src/main/res/values-w750dp/integers.xml

This file was deleted.

5 changes: 0 additions & 5 deletions Source/Android/app/src/main/res/values-w820dp/dimens.xml

This file was deleted.

4 changes: 1 addition & 3 deletions Source/Android/app/src/main/res/values/dimens.xml
@@ -1,8 +1,6 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>

<dimen name="spacing_small">4dp</dimen>
<dimen name="spacing_medlarge">12dp</dimen>
<dimen name="spacing_large">16dp</dimen>
<dimen name="card_width">140dp</dimen>
</resources>
1 change: 0 additions & 1 deletion Source/Android/app/src/main/res/values/integers.xml
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="game_title_lines">2</integer>
<integer name="game_grid_columns">1</integer>
<integer name="loadsave_state_columns">1</integer>
<integer name="loadsave_state_rows">6</integer>

Expand Down

0 comments on commit eb060c7

Please sign in to comment.