1
1
package com .clock .study .activity ;
2
2
3
+ import android .content .Context ;
3
4
import android .content .res .Resources ;
4
5
import android .os .Bundle ;
5
6
import android .support .v7 .app .AppCompatActivity ;
7
+ import android .support .v7 .view .ContextThemeWrapper ;
6
8
import android .support .v7 .widget .LinearLayoutManager ;
7
9
import android .support .v7 .widget .RecyclerView ;
8
10
import android .util .Log ;
19
21
import com .clock .study .helper .DayNightHelper ;
20
22
import com .clock .study .type .DayNight ;
21
23
24
+ import java .lang .reflect .Field ;
25
+ import java .lang .reflect .InvocationTargetException ;
26
+ import java .lang .reflect .Method ;
22
27
import java .util .ArrayList ;
23
28
import java .util .List ;
24
29
@@ -125,9 +130,8 @@ private void changeThemeByJianShu() {
125
130
Resources .Theme theme = getTheme ();
126
131
127
132
theme .resolveAttribute (R .attr .clockBackground , typedValue , true );
128
- Log .i (TAG , "clockBackground resourceId: " + typedValue .resourceId );
129
133
mHeaderLayout .setBackgroundResource (typedValue .resourceId );
130
- mRecyclerView .setBackgroundResource (typedValue .resourceId );
134
+ // mRecyclerView.setBackgroundResource(typedValue.resourceId);
131
135
for (RelativeLayout layout : mLayoutList ) {
132
136
layout .setBackgroundResource (typedValue .resourceId );
133
137
}
@@ -139,9 +143,39 @@ private void changeThemeByJianShu() {
139
143
}
140
144
141
145
theme .resolveAttribute (R .attr .clockTextColor , typedValue , true );
142
- Log . i ( TAG , "clockTextColor resourceId: " + typedValue . resourceId );
146
+ Resources resources = getResources ( );
143
147
for (TextView textView : mTextViewList ) {
144
- textView .setTextColor (getResources ().getColor (typedValue .resourceId ));
148
+ textView .setTextColor (resources .getColor (typedValue .resourceId ));
149
+ }
150
+
151
+ Class <RecyclerView > recyclerViewClass = RecyclerView .class ;
152
+ try {
153
+ Field declaredField = recyclerViewClass .getDeclaredField ("mRecycler" );
154
+ declaredField .setAccessible (true );
155
+ Method declaredMethod = Class .forName (RecyclerView .Recycler .class .getName ()).getDeclaredMethod ("clear" , (Class <?>[]) new Class [0 ]);
156
+ declaredMethod .setAccessible (true );
157
+ declaredMethod .invoke (declaredField .get (mRecyclerView ), new Object [0 ]);
158
+ RecyclerView .RecycledViewPool recycledViewPool = mRecyclerView .getRecycledViewPool ();
159
+ recycledViewPool .clear ();
160
+ Context context = mRecyclerView .getContext ();
161
+ if (context instanceof ContextThemeWrapper ) {
162
+ if (mDayNightHelper .isDay ()) {
163
+ context .setTheme (R .style .DayTheme );
164
+ } else {
165
+ context .setTheme (R .style .NightTheme );
166
+ }
167
+ }
168
+
169
+ } catch (NoSuchFieldException e ) {
170
+ e .printStackTrace ();
171
+ } catch (ClassNotFoundException e ) {
172
+ e .printStackTrace ();
173
+ } catch (NoSuchMethodException e ) {
174
+ e .printStackTrace ();
175
+ } catch (InvocationTargetException e ) {
176
+ e .printStackTrace ();
177
+ } catch (IllegalAccessException e ) {
178
+ e .printStackTrace ();
145
179
}
146
180
}
147
181
0 commit comments