| 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.widget; |
| 18 | |
| 19 | import android.graphics.Canvas; |
| 20 | import android.os.Build; |
| 21 | import android.view.View; |
| 22 | |
| 23 | import androidx.core.view.ViewCompat; |
| 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 | static final ItemTouchUIUtil INSTANCE = new ItemTouchUIUtilImpl(); |
| 31 | |
| 32 | @Override |
| 33 | public void onDraw(Canvas c, RecyclerView recyclerView, View view, float dX, float dY, |
| 34 | int actionState, boolean isCurrentlyActive) { |
| 35 | if (Build.VERSION.SDK_INT >= 21) { |
| 36 | if (isCurrentlyActive) { |
| 37 | Object originalElevation = view.getTag(); |
| 38 | if (originalElevation == null) { |
| 39 | originalElevation = ViewCompat.getElevation(view); |
| 40 | float newElevation = 1f + findMaxElevation(recyclerView, view); |
| 41 | ViewCompat.setElevation(view, newElevation); |
| 42 | view.setTag(originalElevation); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | view.setTranslationX(dX); |
| 48 | view.setTranslationY(dY); |
| 49 | } |
| 50 | |
| 51 | private static float findMaxElevation(RecyclerView recyclerView, View itemView) { |
| 52 | final int childCount = recyclerView.getChildCount(); |
| 53 | float max = 0; |
| 54 | for (int i = 0; i < childCount; i++) { |
| 55 | final View child = recyclerView.getChildAt(i); |
| 56 | if (child == itemView) { |
| 57 | continue; |
| 58 | } |
| 59 | final float elevation = ViewCompat.getElevation(child); |
| 60 | if (elevation > max) { |
| 61 | max = elevation; |
| 62 | } |
| 63 | } |
| 64 | return max; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void onDrawOver(Canvas c, RecyclerView recyclerView, View view, float dX, float dY, |
| 69 | int actionState, boolean isCurrentlyActive) { |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void clearView(View view) { |
| 74 | if (Build.VERSION.SDK_INT >= 21) { |
| 75 | final Object tag = view.getTag(); |
| 76 | if (tag instanceof Float) { |
| 77 | ViewCompat.setElevation(view, (Float) tag); |
| 78 | } |
| 79 | view.setTag(null); |
| 80 | } |
| 81 | |
| 82 | view.setTranslationX(0f); |
| 83 | view.setTranslationY(0f); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void onSelected(View view) { |
| 88 | } |
| 89 | } |
| 90 | |