美文网首页
recyclerView 实现gridView,并有序间隔弹出

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

作者: 貌似很有道理呢 | 来源:发表于2017-05-02 19:00 被阅读123次

先上效果图

<img src="https://img.haomeiwen.com/i2438860/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

相关文章

网友评论

      本文标题:recyclerView 实现gridView,并有序间隔弹出

      本文链接:https://www.haomeiwen.com/subject/sdhhtxtx.html