recyclerView 實(shí)現(xiàn)gridView,并有序間隔彈出

先上效果圖

<img src="http://upload-images.jianshu.io/upload_images/2438860-fd8275770adacc86.gif?imageMogr2/auto-orient/strip" width="100" height="100"/>
首先recyclerView是支持動(dòng)畫的

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、布局文件中添加動(dòng)畫

`

     <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"/> `  

自定義彈出動(dòng)畫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()函數(shù),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
最后編輯于
?著作權(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)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,113評論 25 709
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,879評論 2 45
  • 關(guān)鍵詞密度分析:http://tool.chinaz.com/Tools/Density.aspx關(guān)鍵詞報(bào)告:ht...
    lfpwhy閱讀 268評論 0 0
  • 夢里花落還有誰知? 夢里花開還有誰憶? 漫步于心間流觴 水無痕,月落華年 愛無邊,末之未央 一縷殘陽,余暉映明月 ...
    Angely_Sufi閱讀 232評論 0 1
  • 老媽說我不要強(qiáng)
    清風(fēng)田田閱讀 119評論 0 0

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