summaryrefslogtreecommitdiff
path: root/Source/Android/app/src/main/java
diff options
context:
space:
mode:
authorCharles Lombardo <clombardo169@gmail.com>2022-11-03 16:53:41 -0400
committerCharles Lombardo <clombardo169@gmail.com>2022-11-13 15:49:29 -0500
commiteb060c73567ea5ef93ca4848f003c620d4ec6a0c (patch)
tree09459882a166241396d1a94f3b8b8885b7e1b54e /Source/Android/app/src/main/java
parentf614f94568ca62d65ae94e37d5819822f7e0645f (diff)
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.
Diffstat (limited to 'Source/Android/app/src/main/java')
-rw-r--r--Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/platform/PlatformGamesFragment.java33
1 files changed, 26 insertions, 7 deletions
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());