Home | History | Annotate | Download | only in util
      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 
     17 package com.android.setupwizardlib.util;
     18 
     19 import android.os.Build;
     20 import android.widget.AbsListView;
     21 import android.widget.ListAdapter;
     22 import android.widget.ListView;
     23 
     24 import com.android.setupwizardlib.view.NavigationBar;
     25 
     26 /**
     27  * Add this helper to require the list view to be scrolled to the bottom, making sure that the
     28  * user sees all content on the screen. This will change the navigation bar to show the more button
     29  * instead of the next button when there is more content to be seen. When the more button is
     30  * clicked, the list view will be scrolled one page down.
     31  */
     32 public class ListViewRequireScrollHelper extends AbstractRequireScrollHelper
     33         implements AbsListView.OnScrollListener {
     34 
     35     public static void requireScroll(NavigationBar navigationBar, ListView listView) {
     36         new ListViewRequireScrollHelper(navigationBar, listView).requireScroll();
     37     }
     38 
     39     private final ListView mListView;
     40 
     41     private ListViewRequireScrollHelper(NavigationBar navigationBar, ListView listView) {
     42         super(navigationBar);
     43         mListView = listView;
     44     }
     45 
     46     @Override
     47     protected void requireScroll() {
     48         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
     49             // APIs to scroll a list only exists on Froyo or above.
     50             super.requireScroll();
     51             mListView.setOnScrollListener(this);
     52 
     53             final ListAdapter adapter = mListView.getAdapter();
     54             if (mListView.getLastVisiblePosition() < adapter.getCount()) {
     55                 notifyRequiresScroll();
     56             }
     57         }
     58     }
     59 
     60     @Override
     61     protected void pageScrollDown() {
     62         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
     63             final int height = mListView.getHeight();
     64             mListView.smoothScrollBy(height, 500);
     65         }
     66     }
     67 
     68     @Override
     69     public void onScrollStateChanged(AbsListView view, int scrollState) {
     70     }
     71 
     72     @Override
     73     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
     74             int totalItemCount) {
     75         if (firstVisibleItem + visibleItemCount >= totalItemCount) {
     76             notifyScrolledToBottom();
     77         } else {
     78             notifyRequiresScroll();
     79         }
     80     }
     81 }
     82