1. 程式人生 > >Android實現CoordinatorLayout、RecyclerView返回頂部效果

Android實現CoordinatorLayout、RecyclerView返回頂部效果

一、CoordinatorLayout返回頂部:

 CoordinatorLayout.Behavior behavior =
                ((CoordinatorLayout.LayoutParams) indexAppBar.getLayoutParams()).getBehavior();
        if (behavior instanceof AppBarLayout.Behavior) {
            AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout.Behavior) behavior;
int topAndBottomOffset = appBarLayoutBehavior.getTopAndBottomOffset(); if (topAndBottomOffset != 0) { appBarLayoutBehavior.setTopAndBottomOffset(0); } }

二、RecyclerView返回頂部

 recyclerView.scrollToPosition(0);

注:CoordinatorLayout一定要和AppBarLayout一起使用喲