From eb060c73567ea5ef93ca4848f003c620d4ec6a0c Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Thu, 3 Nov 2022 16:53:41 -0400 Subject: [PATCH] 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. --- .../ui/platform/PlatformGamesFragment.java | 33 +++++++++++++++---- .../app/src/main/res/layout/fragment_grid.xml | 4 +-- .../src/main/res/values-w1000dp/integers.xml | 4 --- .../src/main/res/values-w1050dp/dimens.xml | 6 ---- .../src/main/res/values-w300dp/integers.xml | 4 --- .../src/main/res/values-w400dp/integers.xml | 4 --- .../src/main/res/values-w500dp/integers.xml | 4 --- .../src/main/res/values-w750dp/integers.xml | 4 --- .../app/src/main/res/values-w820dp/dimens.xml | 5 --- .../app/src/main/res/values/dimens.xml | 4 +-- .../app/src/main/res/values/integers.xml | 1 - 11 files changed, 28 insertions(+), 45 deletions(-) delete mode 100644 Source/Android/app/src/main/res/values-w1000dp/integers.xml delete mode 100644 Source/Android/app/src/main/res/values-w1050dp/dimens.xml delete mode 100644 Source/Android/app/src/main/res/values-w300dp/integers.xml delete mode 100644 Source/Android/app/src/main/res/values-w400dp/integers.xml delete mode 100644 Source/Android/app/src/main/res/values-w500dp/integers.xml delete mode 100644 Source/Android/app/src/main/res/values-w750dp/integers.xml delete mode 100644 Source/Android/app/src/main/res/values-w820dp/dimens.xml diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesFragment.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesFragment.java index 9b3cd0e87f..527b7d659d 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesFragment.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesFragment.java @@ -6,12 +6,12 @@ import android.os.Bundle; 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; @@ -62,11 +62,33 @@ public final class PlatformGamesFragment extends Fragment implements PlatformGam 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)); @@ -75,9 +97,6 @@ public final class PlatformGamesFragment extends Fragment implements PlatformGam mSwipeRefresh.setOnRefreshListener(mOnRefreshListener); - mBinding.gridGames.setLayoutManager(layoutManager); - mBinding.gridGames.setAdapter(mAdapter); - InsetsHelper.setUpList(getContext(), mBinding.gridGames); setRefreshing(GameFileCacheManager.isLoadingOrRescanning()); diff --git a/Source/Android/app/src/main/res/layout/fragment_grid.xml b/Source/Android/app/src/main/res/layout/fragment_grid.xml index 62be5a20ea..16395414c6 100644 --- a/Source/Android/app/src/main/res/layout/fragment_grid.xml +++ b/Source/Android/app/src/main/res/layout/fragment_grid.xml @@ -7,9 +7,7 @@ + android:layout_height="wrap_content"> - - 8 - \ No newline at end of file diff --git a/Source/Android/app/src/main/res/values-w1050dp/dimens.xml b/Source/Android/app/src/main/res/values-w1050dp/dimens.xml deleted file mode 100644 index 92fcb2b668..0000000000 --- a/Source/Android/app/src/main/res/values-w1050dp/dimens.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - 96dp - diff --git a/Source/Android/app/src/main/res/values-w300dp/integers.xml b/Source/Android/app/src/main/res/values-w300dp/integers.xml deleted file mode 100644 index 250e291e9d..0000000000 --- a/Source/Android/app/src/main/res/values-w300dp/integers.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - 2 - diff --git a/Source/Android/app/src/main/res/values-w400dp/integers.xml b/Source/Android/app/src/main/res/values-w400dp/integers.xml deleted file mode 100644 index 4148b62c39..0000000000 --- a/Source/Android/app/src/main/res/values-w400dp/integers.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - 3 - diff --git a/Source/Android/app/src/main/res/values-w500dp/integers.xml b/Source/Android/app/src/main/res/values-w500dp/integers.xml deleted file mode 100644 index 5cd4e24f38..0000000000 --- a/Source/Android/app/src/main/res/values-w500dp/integers.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - 4 - \ No newline at end of file diff --git a/Source/Android/app/src/main/res/values-w750dp/integers.xml b/Source/Android/app/src/main/res/values-w750dp/integers.xml deleted file mode 100644 index 4dbc25806e..0000000000 --- a/Source/Android/app/src/main/res/values-w750dp/integers.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - 6 - \ No newline at end of file diff --git a/Source/Android/app/src/main/res/values-w820dp/dimens.xml b/Source/Android/app/src/main/res/values-w820dp/dimens.xml deleted file mode 100644 index d27181e85e..0000000000 --- a/Source/Android/app/src/main/res/values-w820dp/dimens.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - 64dp - diff --git a/Source/Android/app/src/main/res/values/dimens.xml b/Source/Android/app/src/main/res/values/dimens.xml index e0e16c895e..fa4901ee10 100644 --- a/Source/Android/app/src/main/res/values/dimens.xml +++ b/Source/Android/app/src/main/res/values/dimens.xml @@ -1,8 +1,6 @@ - - 16dp - 4dp 12dp 16dp + 140dp diff --git a/Source/Android/app/src/main/res/values/integers.xml b/Source/Android/app/src/main/res/values/integers.xml index 676ebabe7b..b1c1915fa8 100644 --- a/Source/Android/app/src/main/res/values/integers.xml +++ b/Source/Android/app/src/main/res/values/integers.xml @@ -1,7 +1,6 @@ 2 - 1 1 6