• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.documentsui.selection;
17 
18 import static android.support.v4.util.Preconditions.checkArgument;
19 
20 import android.support.v7.widget.RecyclerView;
21 import android.view.MotionEvent;
22 import android.view.View;
23 
24 /**
25  * Provides a means of controlling when and where band selection is initiated.
26  * This can be used to permit band initiation in non-empty areas, like in the whitespace of
27  * a bound view. This is especially useful when there is no empty space between items.
28  */
29 public abstract class BandPredicate {
30 
31     /** @return true if band selection can be initiated in response to the {@link MotionEvent}. */
canInitiate(MotionEvent e)32     public abstract boolean canInitiate(MotionEvent e);
33 
34     /**
35      * A BandPredicate that allows initiation of band selection only in areas of RecyclerView
36      * that have {@link RecyclerView#NO_POSITION}. In most cases, this will be the empty areas
37      * between views.
38      */
39     public static final class NoPositionBandPredicate extends BandPredicate {
40 
41         private final RecyclerView mRecView;
42 
NoPositionBandPredicate(RecyclerView recView)43         public NoPositionBandPredicate(RecyclerView recView) {
44             checkArgument(recView != null);
45 
46             mRecView = recView;
47         }
48 
49         @Override
canInitiate(MotionEvent e)50         public boolean canInitiate(MotionEvent e) {
51             View itemView = mRecView.findChildViewUnder(e.getX(), e.getY());
52             int position = itemView != null
53                     ? mRecView.getChildAdapterPosition(itemView)
54                     : RecyclerView.NO_POSITION;
55 
56             return position == RecyclerView.NO_POSITION;
57         }
58     };
59 }
60