Home | History | Annotate | Download | only in template
      1 /*
      2  * Copyright (C) 2017 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.template;
     18 
     19 import android.support.annotation.NonNull;
     20 import android.support.annotation.Nullable;
     21 import android.util.Log;
     22 import android.widget.AbsListView;
     23 import android.widget.ListAdapter;
     24 import android.widget.ListView;
     25 
     26 import com.android.setupwizardlib.template.RequireScrollMixin.ScrollHandlingDelegate;
     27 
     28 /**
     29  * {@link ScrollHandlingDelegate} which analyzes scroll events from {@link ListView} and
     30  * notifies {@link RequireScrollMixin} about scrollability changes.
     31  */
     32 public class ListViewScrollHandlingDelegate implements ScrollHandlingDelegate,
     33         AbsListView.OnScrollListener {
     34 
     35     private static final String TAG = "ListViewDelegate";
     36 
     37     private static final int SCROLL_DURATION = 500;
     38 
     39     @NonNull
     40     private final RequireScrollMixin mRequireScrollMixin;
     41 
     42     @Nullable
     43     private final ListView mListView;
     44 
     45     public ListViewScrollHandlingDelegate(
     46             @NonNull RequireScrollMixin requireScrollMixin,
     47             @Nullable ListView listView) {
     48         mRequireScrollMixin = requireScrollMixin;
     49         mListView = listView;
     50     }
     51 
     52     @Override
     53     public void startListening() {
     54         if (mListView != null) {
     55             mListView.setOnScrollListener(this);
     56 
     57             final ListAdapter adapter = mListView.getAdapter();
     58             if (mListView.getLastVisiblePosition() < adapter.getCount()) {
     59                 mRequireScrollMixin.notifyScrollabilityChange(true);
     60             }
     61         } else {
     62             Log.w(TAG, "Cannot require scroll. List view is null");
     63         }
     64     }
     65 
     66     @Override
     67     public void pageScrollDown() {
     68         if (mListView != null) {
     69             final int height = mListView.getHeight();
     70             mListView.smoothScrollBy(height, SCROLL_DURATION);
     71         }
     72     }
     73 
     74     @Override
     75     public void onScrollStateChanged(AbsListView view, int scrollState) {
     76     }
     77 
     78     @Override
     79     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
     80             int totalItemCount) {
     81         if (firstVisibleItem + visibleItemCount >= totalItemCount) {
     82             mRequireScrollMixin.notifyScrollabilityChange(false);
     83         } else {
     84             mRequireScrollMixin.notifyScrollabilityChange(true);
     85         }
     86     }
     87 }
     88