BaseRecyclerAdapter之添加動(dòng)畫(策略模式)

上篇「RecyclerView.Adapter優(yōu)化了嗎?」主要講了RecyclerView.Adapter的優(yōu)化代碼以及添加了item的click方法具體實(shí)現(xiàn)原理,這篇在原來的基礎(chǔ)上新增列表動(dòng)畫,后續(xù)還會(huì)擴(kuò)展更多功能,供大家學(xué)習(xí),支持我就Star一下「BaseRecyclerViewAdapterHelper」。

效果如何?

如何使用?

// 一行代碼搞定(默認(rèn)為漸顯效果)
quickAdapter.openLoadAnimation();

如果你想換成別的效果你也可以

// 默認(rèn)提供5種方法(漸顯、縮放、從下到上,從左到右、從右到左)
quickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

如果還是不滿意則可以自定義效果

quickAdapter.openLoadAnimation(new BaseAnimation() {
              @Override
              public Animator[] getAnimators(View view) {
                return new Animator[]{
                    ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1),
                    ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1)
                };
              }
            });

使用就是如此簡單。

如何做到的?

首先先思考兩個(gè)問題

  • 添加動(dòng)畫在哪個(gè)方法里面添加?
  • 如何控制動(dòng)畫加載次數(shù)?

添加動(dòng)畫在哪個(gè)方法里面添加?

onBindViewHolder方法,因?yàn)槊看翁畛鋽?shù)據(jù)的時(shí)候都會(huì)調(diào)用該方法。

private int mDuration = 300;
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    // 此處代碼省略,代碼已經(jīng)在上篇詳細(xì)講解過了
    if (isOpenAnimation) {
            BaseAnimation animation = null;
            if (customAnimation != null) {
                animation = customAnimation;
            }else{
                animation = selectAnimation;
            }
            for (Animator anim : animation.getAnimators(holder.itemView)) {
                anim.setDuration(mDuration).start();
                anim.setInterpolator(mInterpolator);
            }       
        }
}

以上代碼,首先判斷是否開啟動(dòng)畫,然后判斷是否是自定義動(dòng)畫還是用戶選擇的自帶動(dòng)畫,然后對動(dòng)畫的操作元素進(jìn)行遍歷執(zhí)行,執(zhí)行時(shí)間為300毫秒,由于上面說了每次填充數(shù)據(jù)都會(huì)調(diào)用,所以如何不判斷的話,就會(huì)導(dǎo)致上下滑動(dòng)每次都會(huì)重復(fù)調(diào)用動(dòng)畫,動(dòng)畫本身是會(huì)耗費(fèi)性能的。

如何控制動(dòng)畫加載次數(shù)?

private int mDuration = 300;
private int mLastPosition = -1;
private boolean isFirstOnly = true;
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    // 此處代碼省略,代碼已經(jīng)在上篇詳細(xì)講解過了

    if (isOpenAnimation) {
      if (!isFirstOnly || position > mLastPosition) {
            BaseAnimation animation = null;
            if (customAnimation != null) {
                animation = customAnimation;
            }else{
                animation = selectAnimation;
            }
            for (Animator anim : animation.getAnimators(holder.itemView)) {
                anim.setDuration(mDuration).start();
                anim.setInterpolator(mInterpolator);
            }
             mLastPosition = position;       
        }
    }
}
public void setFirstOnly(boolean firstOnly) {
    isFirstOnly = firstOnly;
}

只需要添加一個(gè)mLastPosition來存儲滑動(dòng)過的位置,然后判斷滑動(dòng)的位置是否被滑動(dòng)過,這樣就可以避免每次都添加動(dòng)畫了。不過為了滿足喜歡動(dòng)畫多過于性能的開發(fā)者,如果你想要每次滑動(dòng)都帶動(dòng)畫可以設(shè)置isFirstOnly屬性即可,默認(rèn)是不開啟的。

Animation的設(shè)計(jì)

BaseAnimation

public abstract class BaseAnimation {
    public abstract Animator[] getAnimators(View view);
}

AlphaInAnimation

public class AlphaInAnimation extends BaseAnimation {
    private static final float DEFAULT_ALPHA_FROM = 0f;
    private final float mFrom;
    public AlphaInAnimation() {
        this(DEFAULT_ALPHA_FROM);
    }
    public AlphaInAnimation(float from) {
        mFrom = from;
    }
    @Override
    public Animator[] getAnimators(View view) {
        return new Animator[]{ObjectAnimator.ofFloat(view, "alpha", mFrom, 1f)};
    }
}

策略模式定義了一系列的算法,并將每一個(gè)算法封裝起來,而且使它們還可以相互替換。策略模式讓算法獨(dú)立于使用它的客戶而獨(dú)立變化。

在此采用的是「策略模式」,這樣更加靈活,如果需要自定義一個(gè)自己的動(dòng)畫,只需要繼承BaseAnimation實(shí)現(xiàn)getAnimators方法即可。

public class CustomAnimation extends BaseAnimation {
    @Override 
    public Animator[] getAnimators(View view) {
       return new Animator[]{ 
      ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1), 
      ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1)
     };
 } 
}

設(shè)置即可

quickAdapter.openLoadAnimation(new CustomAnimation());

小知識

openLoadAnimation方法

public void openLoadAnimation(@AnimationType int animationType)

在寫這個(gè)方法的時(shí)候,想是不是用枚舉更合適?但是曾看過多篇性能優(yōu)化的文章不推薦Enum枚舉,因?yàn)樾阅芟?其實(shí)Android有自帶的枚舉
官方文檔說明,安卓開發(fā)應(yīng)避免使用Enum(枚舉類),因?yàn)橄啾扔陟o態(tài)常量Enum會(huì)花費(fèi)兩倍以上的內(nèi)存。
Android推薦,以下寫法

    //先定義 常量
    public static final int SUNDAY = 0;
    public static final int MONDAY = 1;
    public static final int TUESDAY = 2;
    public static final int WEDNESDAY = 3;
    public static final int THURSDAY = 4;
    public static final int FRIDAY = 5;
    public static final int SATURDAY = 6;
    //默認(rèn)參數(shù)
    @WeekDays int currentDay = SUNDAY;

    //用@IntDef "包住" 常量;
    // @Retention 定義策略
    // 聲明構(gòu)造器
    @IntDef({SUNDAY, MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY})
    @Retention(RetentionPolicy.SOURCE)
    public @interface WeekDays {}
    //設(shè)置參數(shù)
    public void setCurrentDay(@WeekDays int currentDay) {
        this.currentDay = currentDay;
    }

詳細(xì)文章可查看

下拉框的引用

aar包是 Android 的類庫項(xiàng)目的二進(jìn)制發(fā)行包。

下拉框引用到別人的工程,引用方式是「arr包」


每次收獲一點(diǎn)點(diǎn),后續(xù)還會(huì)擴(kuò)展更多功能,供大家學(xué)習(xí),支持我就Star一下「[BaseRecyclerViewAdapterHelper]
(https://github.com/CymChad/BaseRecyclerViewAdapterHelper)」。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容