package com.dl7.mvp.adapter;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import jp.wasabeef.recyclerview.adapters.AnimationAdapter;
import static com.dl7.recycler.adapter.BaseQuickAdapter.EMPTY_VIEW;
import static com.dl7.recycler.adapter.BaseQuickAdapter.FOOTER_VIEW;
import static com.dl7.recycler.adapter.BaseQuickAdapter.HEADER_VIEW;
import static com.dl7.recycler.adapter.BaseQuickAdapter.LOADING_VIEW;
/**
* Created by Rukey7 on 2016/12/1.
*/
public class SlideInBottomAdapter extends AnimationAdapter {
public SlideInBottomAdapter(RecyclerView.Adapter<RecyclerView.ViewHolder> adapter) {
super(adapter);
}
@Override protected Animator[] getAnimators(View view) {
return new Animator[] {
ObjectAnimator.ofFloat(view, "translationY", view.getMeasuredHeight(), 0)
};
}
/**
* Called when a view created by this adapter has been attached to a window.
* simple to solve item will layout using all
* {@link #_setFullSpan(RecyclerView.ViewHolder)}
*
* @param holder
*/
@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
super.onViewAttachedToWindow(holder);
int type = holder.getItemViewType();
if (type == EMPTY_VIEW || type == HEADER_VIEW || type == FOOTER_VIEW || type == LOADING_VIEW) {
_setFullSpan(holder);
}
}
/**
* When set to true, the item will layout using all span area. That means, if orientation
* is vertical, the view will have full width; if orientation is horizontal, the view will
* have full height.
* if the hold view use StaggeredGridLayoutManager they should using all span area
*
* @param holder True if this item should traverse all spans.
*/
protected void _setFullSpan(RecyclerView.ViewHolder holder) {
if (holder.itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) {
StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
params.setFullSpan(true);
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if (manager instanceof GridLayoutManager) {
final GridLayoutManager gridManager = ((GridLayoutManager) manager);
gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int type = getItemViewType(position);
return (type == EMPTY_VIEW || type == HEADER_VIEW || type == FOOTER_VIEW || type == LOADING_VIEW) ? gridManager.getSpanCount() : 1;
}
});
}
}
}