Home | History | Annotate | Download | only in drawer
      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 androidx.wear.widget.drawer;
     18 
     19 import android.widget.AbsListView;
     20 import android.widget.AbsListView.OnScrollListener;
     21 
     22 import androidx.annotation.RestrictTo;
     23 import androidx.annotation.RestrictTo.Scope;
     24 import androidx.wear.widget.drawer.FlingWatcherFactory.FlingListener;
     25 import androidx.wear.widget.drawer.FlingWatcherFactory.FlingWatcher;
     26 
     27 import java.lang.ref.WeakReference;
     28 
     29 /**
     30  * {@link FlingWatcher} implementation for {@link AbsListView AbsListViews}. Detects the end of
     31  * a Fling by waiting until the scroll state is no longer {@link
     32  * OnScrollListener#SCROLL_STATE_FLING}.
     33  *
     34  * @hide
     35  */
     36 @RestrictTo(Scope.LIBRARY)
     37 class AbsListViewFlingWatcher implements FlingWatcher, OnScrollListener {
     38 
     39     private final FlingListener mListener;
     40     private final WeakReference<AbsListView> mListView;
     41 
     42     AbsListViewFlingWatcher(FlingListener listener, AbsListView listView) {
     43         mListener = listener;
     44         mListView = new WeakReference<>(listView);
     45     }
     46 
     47     @Override
     48     public void watch() {
     49         AbsListView absListView = mListView.get();
     50         if (absListView != null) {
     51             absListView.setOnScrollListener(this);
     52         }
     53     }
     54 
     55     @Override
     56     public void onScrollStateChanged(AbsListView view, int scrollState) {
     57         if (scrollState != OnScrollListener.SCROLL_STATE_FLING) {
     58             view.setOnScrollChangeListener(null);
     59             mListener.onFlingComplete(view);
     60         }
     61     }
     62 
     63     @Override
     64     public void onScroll(
     65             AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {}
     66 }
     67