如何从MainActivity修改RecycleViewHolder中TextView的文字颜色?
Hey there, let's get this text visibility issue sorted out! The problem you're hitting makes total sense—here's why your initial findViewById attempt didn't work, plus a few practical fixes:
Why Your First Approach Failed
RecyclerView items are dynamically created by your Adapter and ViewHolder, not part of your MainActivity's static layout. When you call findViewById in MainActivity, it only looks for views directly in the Activity's layout hierarchy—those RecyclerView child TextViews haven't been inflated yet (or are nested too deep), so the call returns null, and your color change doesn't apply.
Solutions to Try
1. Use Theme/Style Binding (Most Maintainable)
This is the cleanest way—let Android's theme system handle color switching automatically:
- First, define light/dark themes in
res/values/styles.xml(andres/values-night/styles.xmlif you use day-night themes):<!-- Light theme --> <style name="AppTheme.Light" parent="Theme.MaterialComponents.Light.NoActionBar"> <item name="android:textColorPrimary">@color/black</item> <item name="android:windowBackground">@color/white</item> </style> <!-- Dark theme --> <style name="AppTheme.Dark" parent="Theme.MaterialComponents.Dark.NoActionBar"> <item name="android:textColorPrimary">@color/white</item> <item name="android:windowBackground">@color/black</item> </style> - In your item layout (the XML loaded by RecyclerViewHolder), set your TextView's text color to use the theme attribute:
<TextView ... android:textColor="?android:textColorPrimary" /> - When the user toggles dark mode in preferences, switch the app theme in MainActivity and refresh (or restart the Activity to apply changes fully):
// Get preference state SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); boolean isDarkMode = prefs.getBoolean("dark_mode_key", false); // Set theme setTheme(isDarkMode ? R.style.AppTheme.Dark : R.style.AppTheme.Light); // If you don't want to restart, refresh your RecyclerView adapter yourRecyclerView.getAdapter().notifyDataSetChanged();
2. Dynamically Set Color in ViewHolder/Adapter
If you need more control, adjust the text color directly when binding each RecyclerView item:
- In your Adapter's
onBindViewHoldermethod, check the current dark mode preference and set the text color:@Override public void onBindViewHolder(RecyclerViewHolder holder, int position) { // Get your data YourDataModel item = dataList.get(position); holder.textView.setText(item.getContent()); // Check dark mode state SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(holder.itemView.getContext()); boolean isDarkMode = prefs.getBoolean("dark_mode_key", false); // Set text color holder.textView.setTextColor(isDarkMode ? Color.WHITE : Color.BLACK); } - Make sure to call
notifyDataSetChanged()on your Adapter after the user toggles the preference, so all existing items update their text color.
3. Use a Color State List
Another option is to create a color selector that adapts to the theme:
- Create
res/color/text_color_selector.xml:<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/white" android:theme="@style/AppTheme.Dark"/> <item android:color="@color/black" android:theme="@style/AppTheme.Light"/> </selector> - Then set this as your TextView's text color in the item layout:
<TextView ... android:textColor="@color/text_color_selector" /> - This will automatically switch colors based on the active theme.
Quick Tip
After changing the preference, don't forget to trigger a refresh of your RecyclerView—either by calling adapter.notifyDataSetChanged() or restarting the Activity, so all existing items pick up the new color settings.
内容的提问来源于stack exchange,提问作者Antun Jovanić

