Home | History | Annotate | Download | only in helper
      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.internal.widget.helper;
     18 
     19 import android.graphics.Canvas;
     20 import android.view.View;
     21 
     22 import com.android.internal.R;
     23 import com.android.internal.widget.RecyclerView;
     24 
     25 /**
     26  * Package private class to keep implementations. Putting them inside ItemTouchUIUtil makes them
     27  * public API, which is not desired in this case.
     28  */
     29 class ItemTouchUIUtilImpl implements ItemTouchUIUtil {
     30     @Override
     31     public void onDraw(Canvas c, RecyclerView recyclerView, View view,
     32             float dX, float dY, int actionState, boolean isCurrentlyActive) {
     33         if (isCurrentlyActive) {
     34             Object originalElevation = view.getTag(
     35                     R.id.item_touch_helper_previous_elevation);
     36             if (originalElevation == null) {
     37                 originalElevation = view.getElevation();
     38                 float newElevation = 1f + findMaxElevation(recyclerView, view);
     39                 view.setElevation(newElevation);
     40                 view.setTag(R.id.item_touch_helper_previous_elevation,
     41                         originalElevation);
     42             }
     43         }
     44         view.setTranslationX(dX);
     45         view.setTranslationY(dY);
     46     }
     47 
     48     private float findMaxElevation(RecyclerView recyclerView, View itemView) {
     49         final int childCount = recyclerView.getChildCount();
     50         float max = 0;
     51         for (int i = 0; i < childCount; i++) {
     52             final View child = recyclerView.getChildAt(i);
     53             if (child == itemView) {
     54                 continue;
     55             }
     56             final float elevation = child.getElevation();
     57             if (elevation > max) {
     58                 max = elevation;
     59             }
     60         }
     61         return max;
     62     }
     63 
     64     @Override
     65     public void clearView(View view) {
     66         final Object tag = view.getTag(
     67                 R.id.item_touch_helper_previous_elevation);
     68         if (tag != null && tag instanceof Float) {
     69             view.setElevation((Float) tag);
     70         }
     71         view.setTag(R.id.item_touch_helper_previous_elevation, null);
     72         view.setTranslationX(0f);
     73         view.setTranslationY(0f);
     74     }
     75 
     76     @Override
     77     public void onSelected(View view) {
     78     }
     79 
     80     @Override
     81     public void onDrawOver(Canvas c, RecyclerView recyclerView,
     82             View view, float dX, float dY, int actionState, boolean isCurrentlyActive) {
     83     }
     84 }
     85