[go: nahoru, domu]

17bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam/*
27bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * Copyright (C) 2016 The Android Open Source Project
37bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam *
47bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * Licensed under the Apache License, Version 2.0 (the "License");
57bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * you may not use this file except in compliance with the License.
67bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * You may obtain a copy of the License at
77bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam *
87bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam *      http://www.apache.org/licenses/LICENSE-2.0
97bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam *
107bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * Unless required by applicable law or agreed to in writing, software
117bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * distributed under the License is distributed on an "AS IS" BASIS,
127bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * See the License for the specific language governing permissions and
147bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * limitations under the License.
157bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam */
167bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
177bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lampackage com.android.setupwizardlib.util;
187bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
197bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport android.support.v7.widget.RecyclerView;
207bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
217bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lamimport com.android.setupwizardlib.view.NavigationBar;
227bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
237bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam/**
247bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * Add this helper to require the recycler view to be scrolled to the bottom, making sure that the
257bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * user sees all content on the screen. This will change the navigation bar to show the more button
267bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * instead of the next button when there is more content to be seen. When the more button is
277bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam * clicked, the scroll view will be scrolled one page down.
287bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam */
297bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lampublic class RecyclerViewRequireScrollHelper extends AbstractRequireScrollHelper {
307bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
317bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    public static void requireScroll(NavigationBar navigationBar, RecyclerView recyclerView) {
327bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        new RecyclerViewRequireScrollHelper(navigationBar, recyclerView).requireScroll();
337bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
347bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
357bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    private final RecyclerView mRecyclerView;
367bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
377bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    private RecyclerViewRequireScrollHelper(NavigationBar navigationBar,
387bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            RecyclerView recyclerView) {
397bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        super(navigationBar);
407bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        mRecyclerView = recyclerView;
417bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
427bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
437bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    protected void requireScroll() {
447bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        super.requireScroll();
457bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
467bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            @Override
477bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
487bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                if (!canScrollDown()) {
497bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                    notifyScrolledToBottom();
507bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                } else {
517bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                    notifyRequiresScroll();
527bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                }
537bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            }
547bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        });
557bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
567bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        if (canScrollDown()) {
577bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam            notifyRequiresScroll();
587bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        }
597bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
607bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
617bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    private boolean canScrollDown() {
627bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        // Compatibility implementation of View#canScrollVertically
637bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        final int offset = mRecyclerView.computeVerticalScrollOffset();
647bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        final int range = mRecyclerView.computeVerticalScrollRange()
657bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam                - mRecyclerView.computeVerticalScrollExtent();
667bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        return range != 0 && offset < range - 1;
677bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
687bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam
697bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    @Override
707bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    protected void pageScrollDown() {
717bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        final int height = mRecyclerView.getHeight();
727bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam        mRecyclerView.smoothScrollBy(0, height);
737bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam    }
747bc6f176937ed369b180fa89f6c311d2801f206cMaurice Lam}
75