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一起使用哟

你可能感兴趣的:(Android)