recyclerView 实现gridView,并有序间隔弹出

先上效果图

<img src="http://upload-images.jianshu.io/upload_images/2438860-fd8275770adacc86.gif?imageMogr2/auto-orient/strip" width="100" height="100"/>
首先recyclerView是支持动画的

1.自定义recyclerView

`

public class GridRecyclerView extends RecyclerView {
public GridRecyclerView(Context context) {
    super(context);
}
public GridRecyclerView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public GridRecyclerView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public void setLayoutManager(LayoutManager layout) {
    if (layout instanceof GridLayoutManager) {
        super.setLayoutManager(layout);
    } else {
        throw new ClassCastException("You should only use a GridLayoutManager with GridRecyclerView.");
    }
}

@Override
protected void attachLayoutAnimationParameters(View child, @NonNull ViewGroup.LayoutParams params, int index, int count) {

    if (getAdapter() != null && getLayoutManager() instanceof GridLayoutManager) {

        GridLayoutAnimationController.AnimationParameters animationParams =
                (GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;

        if (animationParams == null) {
            animationParams = new GridLayoutAnimationController.AnimationParameters();
            params.layoutAnimationParameters = animationParams;
        }

        int columns = ((GridLayoutManager) getLayoutManager()).getSpanCount();

        animationParams.count = count;
        animationParams.index = index;
        animationParams.columnsCount = columns;
        animationParams.rowsCount = count / columns;

        final int invertedIndex = count - 1 - index;
        animationParams.column = columns - 1 - (invertedIndex % columns);
        animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;

    } else {
        super.attachLayoutAnimationParameters(child, params, index, count);
    }
}

}`
2、布局文件中添加动画

`

     <com.xxxx.GridRecyclerViewandroid:id="@+id/recycler"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/jdrjdrshare_cancel"
    android:layoutAnimation="@anim/grid_layout_animation"
    android:overScrollMode="never"
    android:paddingBottom="60dp"/> `  

自定义弹出动画grid_layout_animation.xml
`

<gridLayoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/slide_in_bottom"
android:animationOrder="normal"
android:columnDelay="15%"
android:rowDelay="15%"
android:direction="top_to_bottom|left_to_right"/>`  

3.在Activity中
`

 private void setRecyclerAdapter(RecyclerView recyclerView) {
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, mChannelCtrls);
    adapter.setOnItemClickListener(mOnItemClickListener);
    recyclerView.setAdapter(adapter);
}
  @Override
public void onEnterAnimationComplete() {
    super.onEnterAnimationComplete();
    setRecyclerAdapter(recyclerView);
    recyclerView.scheduleLayoutAnimation();
}   `

对onEnterAnimationComplete()函数,google给出的定义是
/**
* Activities cannot draw during the period that their windows are animating in. In order
* to know when it is safe to begin drawing they can override this method which will be
* called when the entering animation has completed.
*/
但onEnterAnimationComplete()方法只有在21之后才存在,对于低于21版本的SDK,使用
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { setRecyclerAdapter(recyclerView); }

参考原文 http://www.tuicool.com/articles/fQZbqu
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,446评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,568评论 2 45
  • 关键词密度分析:http://tool.chinaz.com/Tools/Density.aspx关键词报告:ht...
    lfpwhy阅读 231评论 0 0
  • 梦里花落还有谁知? 梦里花开还有谁忆? 漫步于心间流觞 水无痕,月落华年 爱无边,末之未央 一缕残阳,余晖映明月 ...
    Angely_Sufi阅读 209评论 0 1
  • 老妈说我不要强
    清风田田阅读 103评论 0 0