1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.setupwizardlib.util; 18 19import android.support.v7.widget.RecyclerView; 20 21import com.android.setupwizardlib.view.NavigationBar; 22 23/** 24 * Add this helper to require the recycler view to be scrolled to the bottom, making sure that the 25 * user sees all content on the screen. This will change the navigation bar to show the more button 26 * instead of the next button when there is more content to be seen. When the more button is 27 * clicked, the scroll view will be scrolled one page down. 28 */ 29public class RecyclerViewRequireScrollHelper extends AbstractRequireScrollHelper { 30 31 public static void requireScroll(NavigationBar navigationBar, RecyclerView recyclerView) { 32 new RecyclerViewRequireScrollHelper(navigationBar, recyclerView).requireScroll(); 33 } 34 35 private final RecyclerView mRecyclerView; 36 37 private RecyclerViewRequireScrollHelper(NavigationBar navigationBar, 38 RecyclerView recyclerView) { 39 super(navigationBar); 40 mRecyclerView = recyclerView; 41 } 42 43 protected void requireScroll() { 44 super.requireScroll(); 45 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 46 @Override 47 public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 48 if (!canScrollDown()) { 49 notifyScrolledToBottom(); 50 } else { 51 notifyRequiresScroll(); 52 } 53 } 54 }); 55 56 if (canScrollDown()) { 57 notifyRequiresScroll(); 58 } 59 } 60 61 private boolean canScrollDown() { 62 // Compatibility implementation of View#canScrollVertically 63 final int offset = mRecyclerView.computeVerticalScrollOffset(); 64 final int range = mRecyclerView.computeVerticalScrollRange() 65 - mRecyclerView.computeVerticalScrollExtent(); 66 return range != 0 && offset < range - 1; 67 } 68 69 @Override 70 protected void pageScrollDown() { 71 final int height = mRecyclerView.getHeight(); 72 mRecyclerView.smoothScrollBy(0, height); 73 } 74} 75