Home | History | Annotate | Download | only in selection
      1 /*
      2  * Copyright 2018 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.recyclerview.selection;
     18 
     19 import static androidx.core.util.Preconditions.checkArgument;
     20 
     21 import android.view.MotionEvent;
     22 
     23 import androidx.annotation.Nullable;
     24 import androidx.recyclerview.widget.RecyclerView;
     25 import androidx.recyclerview.widget.RecyclerView.OnItemTouchListener;
     26 
     27 /**
     28  * OnItemTouchListener that delegates drag events to a drag listener,
     29  * else sends event to fallback {@link OnItemTouchListener}.
     30  *
     31  * <p>See {@link OnDragInitiatedListener} for details on implementing drag and drop.
     32  */
     33 final class PointerDragEventInterceptor implements OnItemTouchListener {
     34 
     35     private final ItemDetailsLookup mEventDetailsLookup;
     36     private final OnDragInitiatedListener mDragListener;
     37     private @Nullable OnItemTouchListener mDelegate;
     38 
     39     PointerDragEventInterceptor(
     40             ItemDetailsLookup eventDetailsLookup,
     41             OnDragInitiatedListener dragListener,
     42             @Nullable OnItemTouchListener delegate) {
     43 
     44         checkArgument(eventDetailsLookup != null);
     45         checkArgument(dragListener != null);
     46 
     47         mEventDetailsLookup = eventDetailsLookup;
     48         mDragListener = dragListener;
     49         mDelegate = delegate;
     50     }
     51 
     52     @Override
     53     public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
     54         if (MotionEvents.isPointerDragEvent(e) && mEventDetailsLookup.inItemDragRegion(e)) {
     55             return mDragListener.onDragInitiated(e);
     56         } else if (mDelegate != null) {
     57             return mDelegate.onInterceptTouchEvent(rv, e);
     58         }
     59         return false;
     60     }
     61 
     62     @Override
     63     public void onTouchEvent(RecyclerView rv, MotionEvent e) {
     64         if (mDelegate != null) {
     65             mDelegate.onTouchEvent(rv, e);
     66         }
     67     }
     68 
     69     @Override
     70     public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
     71         if (mDelegate != null) {
     72             mDelegate.onRequestDisallowInterceptTouchEvent(disallowIntercept);
     73         }
     74     }
     75 }
     76