Android: Dynamically adapt grid span to card_game size

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.
This commit is contained in:
Charles Lombardo 2022-11-03 16:53:41 -04:00
parent f614f94568
commit eb060c7356
11 changed files with 28 additions and 45 deletions

View File

@ -6,12 +6,12 @@ import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.android.material.color.MaterialColors; import com.google.android.material.color.MaterialColors;
@ -62,11 +62,33 @@ public final class PlatformGamesFragment extends Fragment implements PlatformGam
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) public void onViewCreated(@NonNull View view, Bundle savedInstanceState)
{ {
mSwipeRefresh = mBinding.swipeRefresh; mSwipeRefresh = mBinding.swipeRefresh;
int columns = getResources().getInteger(R.integer.game_grid_columns);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), columns);
mAdapter = new GameAdapter(requireActivity()); 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 // Set theme color to the refresh animation's background
mSwipeRefresh.setProgressBackgroundColorSchemeColor( mSwipeRefresh.setProgressBackgroundColorSchemeColor(
MaterialColors.getColor(mSwipeRefresh, R.attr.colorPrimary)); MaterialColors.getColor(mSwipeRefresh, R.attr.colorPrimary));
@ -75,9 +97,6 @@ public final class PlatformGamesFragment extends Fragment implements PlatformGam
mSwipeRefresh.setOnRefreshListener(mOnRefreshListener); mSwipeRefresh.setOnRefreshListener(mOnRefreshListener);
mBinding.gridGames.setLayoutManager(layoutManager);
mBinding.gridGames.setAdapter(mAdapter);
InsetsHelper.setUpList(getContext(), mBinding.gridGames); InsetsHelper.setUpList(getContext(), mBinding.gridGames);
setRefreshing(GameFileCacheManager.isLoadingOrRescanning()); setRefreshing(GameFileCacheManager.isLoadingOrRescanning());

View File

@ -7,9 +7,7 @@
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh" android:id="@+id/swipe_refresh"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content">
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/grid_games" android:id="@+id/grid_games"

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="game_grid_columns">8</integer>
</resources>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 1024dp of available width. -->
<dimen name="activity_horizontal_margin">96dp</dimen>
</resources>

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="game_grid_columns">2</integer>
</resources>

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="game_grid_columns">3</integer>
</resources>

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="game_grid_columns">4</integer>
</resources>

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="game_grid_columns">6</integer>
</resources>

View File

@ -1,5 +0,0 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>

View File

@ -1,8 +1,6 @@
<resources> <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_small">4dp</dimen>
<dimen name="spacing_medlarge">12dp</dimen> <dimen name="spacing_medlarge">12dp</dimen>
<dimen name="spacing_large">16dp</dimen> <dimen name="spacing_large">16dp</dimen>
<dimen name="card_width">140dp</dimen>
</resources> </resources>

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<integer name="game_title_lines">2</integer> <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_columns">1</integer>
<integer name="loadsave_state_rows">6</integer> <integer name="loadsave_state_rows">6</integer>