summaryrefslogtreecommitdiff
path: root/Source/Android/app/src/main/java
diff options
context:
space:
mode:
authorJosJuice <josjuice@gmail.com>2022-11-16 20:47:59 +0100
committerGitHub <noreply@github.com>2022-11-16 20:47:59 +0100
commitcecd4d15fe476b580966c805463e002f11d3bad1 (patch)
tree7d96d92f528e87e3fc417a9418162415049d1fb2 /Source/Android/app/src/main/java
parent7ce2be93861ba741b5b415afba76e27cea71ac17 (diff)
parenteb060c73567ea5ef93ca4848f003c620d4ec6a0c (diff)
Merge pull request #11254 from t895/adaptive-grid
Android: Dynamically adapt grid span to card_game size
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());