Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> MultipleTheme框架兼容RecyclerView和CardView&RecyclerView緩存機制

MultipleTheme框架兼容RecyclerView和CardView&RecyclerView緩存機制

編輯:關於Android編程

一.引言

最近搞了搞app無縫切換主題功能,github上看了看,流行的還是插件式,好吧。。完了研究下了。

不過今天咱要搞的是MultipleTheme這個框架,也有1000多star了,應該可以吧,於是乎ji動的整到項目裡准備大干一場。。。小弟沉默十分鐘後,納尼?要全部重寫用到的view?!而且沒有兼容RecyclerView,CardView。。。看來不維護了,行吧行吧,既然這樣那就當學習了,順便咱給他兼容一下。於是乎,有了本文~

附上框架地址:https://github.com/dersoncheng/MultipleTheme

具體使用github上作者寫的很清楚,這裡就不多說了。

本文內容:1.此框架技術點。

2.從源碼分析RecyclerView緩存機制。

3.此框架如何兼容RecyclerView和CardView。

二.框架技術點

1.框架調用流程:

1.調用ColorUiUtil類中changeTheme(View view,Theme theme) :通過遞歸方式,從參數view開始遍歷其所有子view,如果遍歷到的view繼承了ColorUiInterface接口,那麼調用其實現的接口方法。

 

 public static void changeTheme(View rootView, Resources.Theme theme) {
        if (rootView instanceof ColorUiInterface) {
            ((ColorUiInterface) rootView).setTheme(theme);
            if (rootView instanceof ViewGroup) {
                int count = ((ViewGroup) rootView).getChildCount();
                for (int i = 0; i < count; i++) {
                    changeTheme(((ViewGroup) rootView).getChildAt(i), theme);
                }
            }
            if (rootView instanceof AbsListView) {
                try {
                    Field localField = AbsListView.class.getDeclaredField("mRecycler");
                    localField.setAccessible(true);
                    Method localMethod = Class.forName("android.widget.AbsListView$RecycleBin").getDeclaredMethod("clear");
                    localMethod.setAccessible(true);
                    localMethod.invoke(localField.get(rootView));
                } catch (NoSuchFieldException e1) {
                    e1.printStackTrace();
                } catch (ClassNotFoundException e2) {
                    e2.printStackTrace();
                } catch (NoSuchMethodException e3) {
                    e3.printStackTrace();
                } catch (IllegalAccessException e4) {
                    e4.printStackTrace();
                } catch (InvocationTargetException e5) {
                    e5.printStackTrace();
                }
            }
        } else {
            if (rootView instanceof ViewGroup) {
                int count = ((ViewGroup) rootView).getChildCount();
                for (int i = 0; i < count; i++) {
                    changeTheme(((ViewGroup) rootView).getChildAt(i), theme);
                }
            }
            if (rootView instanceof AbsListView) {
                try {
                    Field localField = AbsListView.class.getDeclaredField("mRecycler");
                    localField.setAccessible(true);
                    Method localMethod = Class.forName("android.widget.AbsListView$RecycleBin").getDeclaredMethod("clear");
                    localMethod.setAccessible(true);
                    localMethod.invoke(localField.get(rootView));
                } catch (NoSuchFieldException e1) {
                    e1.printStackTrace();
                } catch (ClassNotFoundException e2) {
                    e2.printStackTrace();
                } catch (NoSuchMethodException e3) {
                    e3.printStackTrace();
                } catch (IllegalAccessException e4) {
                    e4.printStackTrace();
                } catch (InvocationTargetException e5) {
                    e5.printStackTrace();
                }
            }
        }
    }

 

2.實現的接口方法,會根據需要改變的屬性調用ViewAttributeUtil類中的各種方法。

如applyBackgroundDrawable(Context,Theme,attrId):最後這個方法會根據Theme獲取到對應的drawable,調用這個view的setBackGroundDrawable()方法實現設置背景。

 

public static void applyBackgroundDrawable(ColorUiInterface ci, Resources.Theme theme, int paramInt) {
        TypedArray ta = theme.obtainStyledAttributes(new int[]{paramInt});
        Drawable drawable = ta.getDrawable(0);
        if (null != ci) {
            (ci.getView()).setBackgroundDrawable(drawable);
        }
        ta.recycle();
    }

 

3.上面applyBackgroundDrawable(Context,Theme,attrId)方法中第三個參數attrid,又是從ViewAttributeUtil類中getBackGroundAttribute(AttributeSet)得到的:

 

public static int getBackgroundAttibute(AttributeSet attr) {
        return getAttributeValue(attr, android.R.attr.background);
    }
public static int getAttributeValue(AttributeSet attr, int paramInt) {
        int value = -1;
        int count = attr.getAttributeCount();
        for (int i = 0; i < count; i++) {
            if (attr.getAttributeNameResource(i) == paramInt) {
                String str = attr.getAttributeValue(i);
                if (null != str && str.startsWith("?")) {
                    value = Integer.valueOf(str.substring(1, str.length())).intValue();
                    return value;
                }
            }
        }
        return value;
    }

2.清空listView緩存,做到listView所有item都成功改變theme

 public static void changeTheme(View rootView, Resources.Theme theme) {
        if (rootView instanceof ColorUiInterface) {
            ((ColorUiInterface) rootView).setTheme(theme);
            if (rootView instanceof ViewGroup) {
                int count = ((ViewGroup) rootView).getChildCount();
                for (int i = 0; i < count; i++) {
                    changeTheme(((ViewGroup) rootView).getChildAt(i), theme);
                }
            }
            if (rootView instanceof AbsListView) {
                try {
                    Field localField = AbsListView.class.getDeclaredField("mRecycler");
                    localField.setAccessible(true);
                    Method localMethod = Class.forName("android.widget.AbsListView$RecycleBin").getDeclaredMethod("clear");
                    localMethod.setAccessible(true);
                    localMethod.invoke(localField.get(rootView));
                } catch 。。。省略異常
            }
        } else {
            if (rootView instanceof ViewGroup) {
                int count = ((ViewGroup) rootView).getChildCount();
                for (int i = 0; i < count; i++) {
                    changeTheme(((ViewGroup) rootView).getChildAt(i), theme);
                }
            }
            if (rootView instanceof AbsListView) {
                try {
                    Field localField = AbsListView.class.getDeclaredField("mRecycler");
                    localField.setAccessible(true);
                    Method localMethod = Class.forName("android.widget.AbsListView$RecycleBin").getDeclaredMethod("clear");
                    localMethod.setAccessible(true);
                    localMethod.invoke(localField.get(rootView));
                } catch。。。省略異常
            }
        }
    }
咱們看上面這個重要的方法:由於ListView也是ViewGroup,並有緩存機制,在換膚後會有緩存的item再次被復用時,此項item沒有改變theme。就會出現當前可見item換了主題,滑動列表,之前不可見的有一部分item的theme沒有發生變化。 於是這方法,通過反射調用了緩存管理類內部類——RecyclerBin中clear()方法,清空所有item,讓listview將所有item重新創建一遍。 那根據這個原理,將RecyclerView中緩存做一下清空。下面先介紹一下RecyclerView緩存機制和兼容。

三.RecyclerView緩存機制

1.RecyclerView緩存機制

RecyclerView中也有管理緩存的內部類:Recycler。LayoutManager獲取Adapter某一項的View時會調用Recycler中的方法獲取。

2.如下是Recyler的幾個關鍵成員變量和方法:

private ArrayList mAttachedScrap:未與RecyclerView分離的ViewHolder列表。

private ArrayList mChangedScrap: 分離的ViewHolder列表。

private ArrayList mCachedViews: ViewHolder緩存列表。會默認緩存

private ViewCacheExtension mViewCacheExtension: 開發者可以控制的ViewHolder緩存。

private RecycledViewPool mRecyclerPool: 提供復用ViewHolder池。

public void bindViewToPosition(View view, int position):將某個View綁定到Adapter的某個位置。

public View getViewForPosition(int position):LayoutManager通過此方法獲取某一項的view。

3.LayoutManager會通過其中getViewForPosition方法獲取緩存的view:獲取緩存view順序是:

(1)mChangedScrap查找,若匹配到則返回相應holder

(2)mAttachedScrap查找,若匹配到且holder有效則返回相應holder

(3)mViewCacheExtension查找,若匹配到則返回相應holder

(4)mRecyclerPool中查找,若匹配到則返回相應holder

(5)還是沒有匹配的,那麼adapter.createViewHolder(),創建新的holder

(6)返回holder.itemView

四.此框架如何兼容RecyclerView和CardView。

1.兼容RecyclerView

為了保證改變所有item的theme,所以要情況緩存。那麼從上面緩存機制知道,我們需要把 mAttachedScrap,mChangedScrap,mCachedViews,mViewCacheExtension,mRecyclerPool中的緩存都清空掉。 在Recycler類中有三個方法可以清空數據: (1)clear() (2)clearScrap() (3)RecycledViewPool中的clear()

兼容後的ColorUiUtil類:

import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class ColorUiUtil {
    
    public static void changeTheme(View rootView, Resources.Theme theme) {
        if (rootView instanceof ColorUiInterface) {
            ((ColorUiInterface) rootView).setTheme(theme);

            if (rootView instanceof RecyclerView) {
                int count = ((RecyclerView) rootView).getChildCount();
                for (int i = 0; i < count; i++) {
                    changeTheme(((ViewGroup) rootView).getChildAt(i), theme);
                }
            }
            else if (rootView instanceof ViewGroup) {
                int count = ((ViewGroup) rootView).getChildCount();
                for (int i = 0; i < count; i++) {
                    changeTheme(((ViewGroup) rootView).getChildAt(i), theme);
                }
            }

            if (rootView instanceof AbsListView) {
                try {
                    Field localField = AbsListView.class.getDeclaredField("mRecycler");
                    localField.setAccessible(true);
                    Method localMethod = Class.forName("android.widget.AbsListView$RecycleBin").getDeclaredMethod("clear");
                    localMethod.setAccessible(true);
                    localMethod.invoke(localField.get(rootView));
                } catch (NoSuchFieldException e1) {
                    e1.printStackTrace();
                } catch (ClassNotFoundException e2) {
                    e2.printStackTrace();
                } catch (NoSuchMethodException e3) {
                    e3.printStackTrace();
                } catch (IllegalAccessException e4) {
                    e4.printStackTrace();
                } catch (InvocationTargetException e5) {
                    e5.printStackTrace();
                }
            } else if (rootView instanceof RecyclerView) {
                try {
              
                    Field localField = RecyclerView.class.getDeclaredField("mRecycler");
                    localField.setAccessible(true);
                    Method localMethod = Class.forName("android.support.v7.widget.RecyclerView$Recycler").getDeclaredMethod("clear");
                    localMethod.setAccessible(true);
                    localMethod.invoke(localField.get(rootView));

                    Method localMethod1 = Class.forName("android.support.v7.widget.RecyclerView$Recycler").getDeclaredMethod("clearScrap");
                    localMethod1.setAccessible(true);
                    localMethod1.invoke(localField.get(rootView));

                    ((RecyclerView) rootView).getRecycledViewPool().clear();

                } catch (NoSuchFieldException e1) {
                    e1.printStackTrace();
                } catch (ClassNotFoundException e2) {
                    e2.printStackTrace();
                } catch (NoSuchMethodException e3) {
                    e3.printStackTrace();
                } catch (IllegalAccessException e4) {
                    e4.printStackTrace();
                } catch (InvocationTargetException e5) {
                    e5.printStackTrace();
                }
            }
        } else {
            if (rootView instanceof RecyclerView) {
                int count = ((RecyclerView) rootView).getChildCount();
                for (int i = 0; i < count; i++) {
                    changeTheme(((ViewGroup) rootView).getChildAt(i), theme);
                }
            }
            else if (rootView instanceof ViewGroup) {
                int count = ((ViewGroup) rootView).getChildCount();
                for (int i = 0; i < count; i++) {
                    changeTheme(((ViewGroup) rootView).getChildAt(i), theme);
                }
            }

            if (rootView instanceof AbsListView) {
                try {
                    Field localField = AbsListView.class.getDeclaredField("mRecycler");
                    localField.setAccessible(true);
                    Method localMethod = Class.forName("android.widget.AbsListView$RecycleBin").getDeclaredMethod("clear");
                    localMethod.setAccessible(true);
                    localMethod.invoke(localField.get(rootView));
                } catch (NoSuchFieldException e1) {
                    e1.printStackTrace();
                } catch (ClassNotFoundException e2) {
                    e2.printStackTrace();
                } catch (NoSuchMethodException e3) {
                    e3.printStackTrace();
                } catch (IllegalAccessException e4) {
                    e4.printStackTrace();
                } catch (InvocationTargetException e5) {
                    e5.printStackTrace();
                }
            } else if (rootView instanceof RecyclerView) {
                try {
                   
                    Field localField = RecyclerView.class.getDeclaredField("mRecycler");
                    localField.setAccessible(true);
                    Method localMethod = Class.forName("android.support.v7.widget.RecyclerView$Recycler").getDeclaredMethod("clear");
                    localMethod.setAccessible(true);
                    localMethod.invoke(localField.get(rootView));

                    Method localMethod1 = Class.forName("android.support.v7.widget.RecyclerView$Recycler").getDeclaredMethod("clearScrap");
                    localMethod1.setAccessible(true);
                    localMethod1.invoke(localField.get(rootView));

                    ((RecyclerView) rootView).getRecycledViewPool().clear();

                } catch (NoSuchFieldException e1) {
                    e1.printStackTrace();
                } catch (ClassNotFoundException e2) {
                    e2.printStackTrace();
                } catch (NoSuchMethodException e3) {
                    e3.printStackTrace();
                } catch (IllegalAccessException e4) {
                    e4.printStackTrace();
                } catch (InvocationTargetException e5) {
                    e5.printStackTrace();
                }
            }
        }
    }
}

3.兼容CardView

cardView設置其背景顏色有自己的方法:

setCardBackgroundColor(Color)
xml中:app:cardBackgroundColor="?attr/colorPrimaryDark"
因為CardView繼承自FrameLayout,所以也可以用setBackGroundDrawable(),但是測試下,在列表中,會出現更改theme無效的bug。

框架中做如下修改:

(1)在ViewAttributeUtil類中增加一個方法:
public static int getCardViewAttribute(AttributeSet attributeSet) {
        return getAttributeValue(attributeSet, android.support.v7.cardview.R.attr.cardBackgroundColor);
    }
(2)在ViewAttributeUtil類中增加一個方法:
public static void applyCardViewBackgroundColor(ColorUiInterface ci, Resources.Theme theme, int paramInt) {
    TypedArray ta = theme.obtainStyledAttributes(new int[]{paramInt});
    int color = ta.getColor(0, Color.GREEN);
    if (null != ci) {
        ((CardView)ci.getView()).setCardBackgroundColor(color);
    }
    ta.recycle();
}
(3)在自定義ColorCardView類中,調用的就是上面兩個方法了。  

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved