1 /*
2  * Copyright (C) 2015 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.example.android.supportv7.widget.touch;
18 
19 import android.graphics.Canvas;
20 import android.view.MotionEvent;
21 import android.view.View;
22 import android.view.ViewGroup;
23 
24 import androidx.core.content.ContextCompat;
25 import androidx.recyclerview.widget.ItemTouchHelper;
26 import androidx.recyclerview.widget.RecyclerView;
27 
28 import com.example.android.supportv7.R;
29 import com.example.android.supportv7.widget.util.ConfigToggle;
30 
31 public class SwipeToDismissActivity extends ItemTouchHelperActivity {
32     boolean mSwipeStartEnabled = true;
33     boolean mSwipeEndEnabled = true;
34     boolean mPointerSwipeEnabled = true;
35     boolean mCustomSwipeEnabled = false;
36 
37     @Override
createConfigToggles()38     ConfigToggle[] createConfigToggles() {
39         ConfigToggle[] configToggles = {
40                 new ConfigToggle(this, R.string.swipe_start) {
41                     @Override
42                     public boolean isChecked() {
43                         return mSwipeStartEnabled;
44                     }
45 
46                     @Override
47                     public void onChange(boolean newValue) {
48                         mSwipeStartEnabled = newValue;
49                     }
50                 },
51                 new ConfigToggle(this, R.string.swipe_end) {
52                     @Override
53                     public boolean isChecked() {
54                         return mSwipeEndEnabled;
55                     }
56 
57                     @Override
58                     public void onChange(boolean newValue) {
59                         mSwipeEndEnabled = newValue;
60                     }
61                 },
62                 new ConfigToggle(this, R.string.pointer_swipe_enabled) {
63                     @Override
64                     public boolean isChecked() {
65                         return mPointerSwipeEnabled;
66                     }
67 
68                     @Override
69                     public void onChange(boolean newValue) {
70                         mPointerSwipeEnabled = newValue;
71                         mAdapter.notifyDataSetChanged();
72                     }
73                 }
74         };
75         ConfigToggle[] copy = new ConfigToggle[configToggles.length + 1];
76         System.arraycopy(configToggles, 0, copy, 0, configToggles.length);
77         copy[copy.length - 1] = new ConfigToggle(this, R.string.custom_swipe_enabled) {
78             @Override
79             public boolean isChecked() {
80                 return mCustomSwipeEnabled;
81             }
82 
83             @Override
84             public void onChange(boolean newValue) {
85                 mCustomSwipeEnabled = newValue;
86             }
87         };
88         return copy;
89     }
90 
91     @Override
onBind(ItemTouchViewHolder viewHolder)92     public void onBind(ItemTouchViewHolder viewHolder) {
93         super.onBind(viewHolder);
94         viewHolder.actionButton.setVisibility(mPointerSwipeEnabled ? View.GONE : View.VISIBLE);
95     }
96 
97     @Override
clearView(RecyclerView.ViewHolder viewHolder)98     public void clearView(RecyclerView.ViewHolder viewHolder) {
99         super.clearView(viewHolder);
100         ItemTouchViewHolder touchVH = (ItemTouchViewHolder) viewHolder;
101         touchVH.cardView.setCardBackgroundColor(
102                 ContextCompat.getColor(this, android.R.color.white));
103         touchVH.overlay.setVisibility(View.GONE);
104     }
105 
106     @Override
onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState)107     public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
108         ItemTouchViewHolder touchVH = (ItemTouchViewHolder) viewHolder;
109         if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
110             touchVH.cardView.setCardBackgroundColor(
111                     ContextCompat.getColor(this, R.color.card_aquatic));
112             if (mCustomSwipeEnabled) {
113                 // hide it
114                 touchVH.overlay.setTranslationX(viewHolder.itemView.getWidth());
115                 touchVH.overlay.setVisibility(View.VISIBLE);
116             }
117         }
118         super.onSelectedChanged(viewHolder, actionState);
119     }
120 
121     @Override
onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive)122     public boolean onChildDraw(Canvas c, RecyclerView recyclerView,
123             RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState,
124             boolean isCurrentlyActive) {
125         if (!mCustomSwipeEnabled) {
126             return false;
127         }
128         ItemTouchViewHolder touchVH = (ItemTouchViewHolder) viewHolder;
129         final float dir = Math.signum(dX);
130         if (dir == 0) {
131             touchVH.overlay.setTranslationX(-touchVH.overlay.getWidth());
132         } else {
133             final float overlayOffset = dX - dir * viewHolder.itemView.getWidth();
134             touchVH.overlay.setTranslationX(overlayOffset);
135         }
136         float alpha = (float) (.2 + .8 * Math.abs(dX) / viewHolder.itemView.getWidth());
137         touchVH.overlay.setAlpha(alpha);
138         return true;
139     }
140 
141     @Override
onCreateViewHolder(ViewGroup parent)142     public ItemTouchViewHolder onCreateViewHolder(ViewGroup parent) {
143         final ItemTouchViewHolder vh = super.onCreateViewHolder(parent);
144         vh.actionButton.setText(R.string.swipe);
145         vh.actionButton.setOnTouchListener(new View.OnTouchListener() {
146             @Override
147             public boolean onTouch(View v, MotionEvent event) {
148                 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
149                     mItemTouchHelper.startSwipe(vh);
150                 }
151                 return false;
152             }
153         });
154         return vh;
155     }
156 
157     @Override
isPointerSwipeEnabled()158     public boolean isPointerSwipeEnabled() {
159         return mPointerSwipeEnabled;
160     }
161 
162     @Override
getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)163     public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
164         return mCallback.makeMovementFlags(0,
165                 (mSwipeStartEnabled ? ItemTouchHelper.START : 0) |
166                         (mSwipeEndEnabled ? ItemTouchHelper.END : 0));
167     }
168 }
169