RecyclerView 夜间模式

自定义View实现主题切换,在此对MultipleTheme框架添加RecyclerView的主题切换,框架就不多介绍,git地址:https://github.com/dersoncheng/MultipleTheme


问题就是RecyclerView 的部分不可见item主题不会及时切换,需要让RecyclerView 缓存在Pool中的item失效


 RecyclerView mRecyclerView = (RecyclerView) rootView;
                    Class recyclerViewClass = RecyclerView.class;
                    try {
                        Field declaredField = recyclerViewClass.getDeclaredField("mRecycler");
                        declaredField.setAccessible(true);
                        Method declaredMethod = Class.forName(RecyclerView.Recycler.class.getName()).getDeclaredMethod("clear", (Class[]) new Class[0]);
                        declaredMethod.setAccessible(true);
                        declaredMethod.invoke(declaredField.get(mRecyclerView), new Object[0]);
                        RecyclerView.RecycledViewPool recycledViewPool = mRecyclerView.getRecycledViewPool();
                        recycledViewPool.clear();

                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }




你可能感兴趣的:(java)