content: package com.rsen.animation.recyclerview.adapters;
  
  import android.animation.Animator;
  import android.support.v7.widget.RecyclerView;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.animation.Interpolator;
  import android.view.animation.LinearInterpolator;
  
  import com.rsen.animation.recyclerview.internal.ViewHelper;
  
  
  /**
   * Copyright (C) 2015 Wasabeef
   * <p>
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * <p>
   * http://www.apache.org/licenses/LICENSE-2.0
   * <p>
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  public abstract class AnimationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  
      long firstTime = 0;
      long loadTime = 0;
      long count = 0;
      boolean enableDelay = false;
      private RecyclerView.Adapter<RecyclerView.ViewHolder> mAdapter;
      private int mDuration = 300;
      private Interpolator mInterpolator = new LinearInterpolator();
      private int mLastPosition = -1;
      private boolean isFirstOnly = true;
  
      public AnimationAdapter(RecyclerView.Adapter<RecyclerView.ViewHolder> adapter) {
          mAdapter = adapter;
      }
  
      @Override
      public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          return mAdapter.onCreateViewHolder(parent, viewType);
      }
  
      @Override
      public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
          super.registerAdapterDataObserver(observer);
          mAdapter.registerAdapterDataObserver(observer);
      }
  
      @Override
      public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
          super.unregisterAdapterDataObserver(observer);
          mAdapter.unregisterAdapterDataObserver(observer);
      }
  
      @Override
      public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
          mAdapter.onBindViewHolder(holder, position);
          loadTime = System.currentTimeMillis();
          int adapterPosition = holder.getAdapterPosition();
          boolean delay = false;
  
          if (!isFirstOnly || adapterPosition > mLastPosition) {
              if ((loadTime - firstTime) < 10) {
                  delay = true;
                  count++;
              } else {
                  count = 0;
              }
              for (Animator anim : getAnimators(holder.itemView)) {
                  anim.setDuration(mDuration).start();
                  if (delay && enableDelay) {
                      anim.setStartDelay(count * 30);
                  }
                  anim.setInterpolator(mInterpolator);
              }
              mLastPosition = adapterPosition;
          } else {
              ViewHelper.clear(holder.itemView);
          }
          firstTime = loadTime;
      }
  
      @Override
      public void onViewRecycled(RecyclerView.ViewHolder holder) {
          mAdapter.onViewRecycled(holder);
          super.onViewRecycled(holder);
      }
  
      @Override
      public int getItemCount() {
          return mAdapter.getItemCount();
      }
  
      public void setDuration(int duration) {
          mDuration = duration;
      }
  
      public void setInterpolator(Interpolator interpolator) {
          mInterpolator = interpolator;
      }
  
      public void setStartPosition(int start) {
          mLastPosition = start;
      }
  
      protected abstract Animator[] getAnimators(View view);
  
      /**
       * 是否动画只显示一次
       */
      public void setFirstOnly(boolean firstOnly) {
          isFirstOnly = firstOnly;
      }
  
      @Override
      public int getItemViewType(int position) {
          return mAdapter.getItemViewType(position);
      }
  
      public RecyclerView.Adapter<RecyclerView.ViewHolder> getWrappedAdapter() {
          return mAdapter;
      }
  
      @Override
      public long getItemId(int position) {
          return mAdapter.getItemId(position);
      }
  }
  