Skip to content

Commit 92a59f9

Browse files
committed
fix: prevent duplicate font scaling and add settings page support
- Fix sliding menu applying scaling multiple times on each open - Use tags to track already scaled views to prevent re-scaling - Save original text size to avoid cumulative scaling - Add font scaling support to PreferenceFragment settings page - Apply scaling to ListView items with scroll listener This ensures font scaling is only applied once per view and works correctly across all UI components including settings.
1 parent 8157ea4 commit 92a59f9

File tree

3 files changed

+104
-8
lines changed

3 files changed

+104
-8
lines changed

app/src/main/java/me/ghui/v2er/module/home/MainActivity.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,36 @@ private void initCheckIn() {
306306
}
307307

308308
private void applyFontSizeToNavigationMenu() {
309-
Menu menu = mNavigationView.getMenu();
310-
float scalingRatio = FontSizeUtil.getScalingRatio();
309+
// Apply font size based on preference
310+
// This is better handled by setting text appearance in styles
311+
// We'll use a post delay to ensure the menu is fully initialized
312+
mNavigationView.postDelayed(() -> {
313+
applyFontScalingToMenuItems();
314+
}, 100);
315+
}
311316

312-
// Apply text scaling to NavigationView items
313-
// NavigationView uses internal text views that we need to scale
317+
private void applyFontScalingToMenuItems() {
318+
// Apply scaling only once to avoid repeated scaling
314319
ViewGroup menuView = (ViewGroup) mNavigationView.getChildAt(0);
315320
if (menuView != null && menuView instanceof RecyclerView) {
316321
RecyclerView recyclerView = (RecyclerView) menuView;
322+
// Apply to currently visible items
323+
for (int i = 0; i < recyclerView.getChildCount(); i++) {
324+
View child = recyclerView.getChildAt(i);
325+
if (child != null && child.getTag(R.id.font_scaled_tag) == null) {
326+
applyScalingToView(child);
327+
child.setTag(R.id.font_scaled_tag, true);
328+
}
329+
}
330+
331+
// Set up listener for future items but check if already scaled
317332
recyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
318333
@Override
319334
public void onChildViewAttachedToWindow(View view) {
320-
applyScalingToView(view);
335+
if (view.getTag(R.id.font_scaled_tag) == null) {
336+
applyScalingToView(view);
337+
view.setTag(R.id.font_scaled_tag, true);
338+
}
321339
}
322340

323341
@Override
@@ -331,9 +349,14 @@ public void onChildViewDetachedFromWindow(View view) {
331349
private void applyScalingToView(View view) {
332350
if (view instanceof TextView) {
333351
TextView textView = (TextView) view;
334-
float currentSize = textView.getTextSize();
335-
float scaledSize = FontSizeUtil.getScaledSize(currentSize);
336-
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, scaledSize);
352+
// Save original size if not already saved
353+
Object originalSize = textView.getTag(R.id.original_text_size_tag);
354+
if (originalSize == null) {
355+
textView.setTag(R.id.original_text_size_tag, textView.getTextSize());
356+
float baseSize = textView.getTextSize();
357+
float scaledSize = FontSizeUtil.getScaledSize(baseSize);
358+
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, scaledSize);
359+
}
337360
} else if (view instanceof ViewGroup) {
338361
ViewGroup viewGroup = (ViewGroup) view;
339362
for (int i = 0; i < viewGroup.getChildCount(); i++) {

app/src/main/java/me/ghui/v2er/module/settings/SettingFragment.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
import androidx.annotation.StringRes;
1111

1212
import android.view.View;
13+
import android.view.ViewGroup;
1314
import android.widget.ListView;
15+
import android.widget.TextView;
16+
import android.widget.Adapter;
17+
import android.widget.ListAdapter;
1418

1519
import me.ghui.v2er.util.Theme;
1620
import me.ghui.v2er.R;
@@ -23,6 +27,7 @@
2327
import me.ghui.v2er.util.FontSizeUtil;
2428
import me.ghui.v2er.util.GlideCatchUtil;
2529
import me.ghui.v2er.util.UserUtils;
30+
import android.util.TypedValue;
2631
import me.ghui.v2er.util.Utils;
2732
import me.ghui.v2er.util.Voast;
2833
import me.ghui.v2er.widget.dialog.ConfirmDialog;
@@ -88,6 +93,70 @@ public void onActivityCreated(Bundle savedInstanceState) {
8893
list.setDivider(null);
8994
// list.setDivider(getActivity().getDrawable(R.drawable.common_divider));
9095
Utils.setPaddingForNavbar(list);
96+
97+
// Apply font scaling to preference items
98+
applyFontScalingToPreferences(list);
99+
}
100+
}
101+
102+
private void applyFontScalingToPreferences(ListView listView) {
103+
// Apply font scaling after a short delay to ensure views are created
104+
listView.postDelayed(() -> {
105+
float scalingRatio = FontSizeUtil.getScalingRatio();
106+
if (Math.abs(scalingRatio - 1.0f) < 0.01f) {
107+
// No scaling needed for default size
108+
return;
109+
}
110+
111+
// Apply to all visible items
112+
for (int i = 0; i < listView.getChildCount(); i++) {
113+
View child = listView.getChildAt(i);
114+
applyFontScalingToView(child);
115+
}
116+
117+
// Add scroll listener to apply scaling to new items
118+
listView.setOnScrollListener(new android.widget.AbsListView.OnScrollListener() {
119+
@Override
120+
public void onScrollStateChanged(android.widget.AbsListView view, int scrollState) {
121+
// Not needed
122+
}
123+
124+
@Override
125+
public void onScroll(android.widget.AbsListView view, int firstVisibleItem,
126+
int visibleItemCount, int totalItemCount) {
127+
// Apply scaling to newly visible items
128+
for (int i = 0; i < visibleItemCount; i++) {
129+
View child = view.getChildAt(i);
130+
if (child != null && child.getTag(R.id.font_scaled_tag) == null) {
131+
applyFontScalingToView(child);
132+
child.setTag(R.id.font_scaled_tag, true);
133+
}
134+
}
135+
}
136+
});
137+
}, 100);
138+
}
139+
140+
private void applyFontScalingToView(View view) {
141+
if (view == null) return;
142+
143+
if (view instanceof TextView) {
144+
TextView textView = (TextView) view;
145+
// Check if already scaled
146+
if (textView.getTag(R.id.original_text_size_tag) == null) {
147+
float originalSize = textView.getTextSize();
148+
textView.setTag(R.id.original_text_size_tag, originalSize);
149+
float scaledSize = FontSizeUtil.getScaledSize(originalSize);
150+
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, scaledSize);
151+
}
152+
}
153+
154+
if (view instanceof ViewGroup) {
155+
ViewGroup viewGroup = (ViewGroup) view;
156+
for (int i = 0; i < viewGroup.getChildCount(); i++) {
157+
View child = viewGroup.getChildAt(i);
158+
applyFontScalingToView(child);
159+
}
91160
}
92161
}
93162

app/src/main/res/values/ids.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@
3030
<item name="inner_toolbar" type="id" />
3131
<item name="base_recyclerview" type="id" />
3232

33+
<!-- Font size scaling tags -->
34+
<item name="font_scaled_tag" type="id" />
35+
<item name="original_text_size_tag" type="id" />
36+
3337
</resources>

0 commit comments

Comments
 (0)