15b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar/* 25b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * Copyright (C) 2015 The Android Open Source Project 35b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * 45b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License"); 55b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * you may not use this file except in compliance with the License. 65b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * You may obtain a copy of the License at 75b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * 85b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * http://www.apache.org/licenses/LICENSE-2.0 95b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * 105b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * Unless required by applicable law or agreed to in writing, software 115b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS, 125b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * See the License for the specific language governing permissions and 145b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * limitations under the License. 155b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar */ 165b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 175b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarpackage com.example.android.supportv7.widget.touch; 185b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 195b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport com.example.android.supportv7.R; 205b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport com.example.android.supportv7.widget.util.ConfigToggle; 215b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 225b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.annotation.TargetApi; 235b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.graphics.Canvas; 245b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.os.Build; 25e8e0eae2600001c8b1e9d5a9f0331cf045193746Aurimas Liutikasimport android.support.v4.content.ContextCompat; 265b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.support.v4.view.MotionEventCompat; 275b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.support.v7.widget.RecyclerView; 285b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.support.v7.widget.helper.ItemTouchHelper; 295b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.view.MotionEvent; 305b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.view.View; 315b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.view.ViewGroup; 325b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 335b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarpublic class SwipeToDismissActivity extends ItemTouchHelperActivity { 345b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar boolean mSwipeStartEnabled = true; 355b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar boolean mSwipeEndEnabled = true; 365b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar boolean mPointerSwipeEnabled = true; 375b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar boolean mCustomSwipeEnabled = false; 385b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 395b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 405b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar ConfigToggle[] createConfigToggles() { 415b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar ConfigToggle[] configToggles = { 425b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar new ConfigToggle(this, R.string.swipe_start) { 435b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 445b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public boolean isChecked() { 455b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar return mSwipeStartEnabled; 465b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 475b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 485b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 495b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public void onChange(boolean newValue) { 505b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar mSwipeStartEnabled = newValue; 515b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 525b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar }, 535b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar new ConfigToggle(this, R.string.swipe_end) { 545b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 555b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public boolean isChecked() { 565b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar return mSwipeEndEnabled; 575b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 585b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 595b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 605b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public void onChange(boolean newValue) { 615b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar mSwipeEndEnabled = newValue; 625b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 635b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar }, 645b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar new ConfigToggle(this, R.string.pointer_swipe_enabled) { 655b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 665b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public boolean isChecked() { 675b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar return mPointerSwipeEnabled; 685b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 695b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 705b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 715b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public void onChange(boolean newValue) { 725b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar mPointerSwipeEnabled = newValue; 735b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar mAdapter.notifyDataSetChanged(); 745b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 755b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 765b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar }; 775b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 785b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar ConfigToggle[] copy = new ConfigToggle[configToggles.length + 1]; 795b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar System.arraycopy(configToggles, 0, copy, 0, configToggles.length); 805b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar copy[copy.length - 1] = new ConfigToggle(this, R.string.custom_swipe_enabled) { 815b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 825b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public boolean isChecked() { 835b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar return mCustomSwipeEnabled; 845b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 855b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 865b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 875b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public void onChange(boolean newValue) { 885b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar mCustomSwipeEnabled = newValue; 895b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 905b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar }; 915b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar return copy; 925b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } else { 935b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar return configToggles; 945b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 955b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 965b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 975b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 985b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public void onBind(ItemTouchViewHolder viewHolder) { 995b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar super.onBind(viewHolder); 1005b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar viewHolder.actionButton.setVisibility(mPointerSwipeEnabled ? View.GONE : View.VISIBLE); 1015b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1025b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 1035b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 1045b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public void clearView(RecyclerView.ViewHolder viewHolder) { 1055b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar super.clearView(viewHolder); 1065b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar ItemTouchViewHolder touchVH = (ItemTouchViewHolder) viewHolder; 107e8e0eae2600001c8b1e9d5a9f0331cf045193746Aurimas Liutikas touchVH.cardView.setCardBackgroundColor( 108e8e0eae2600001c8b1e9d5a9f0331cf045193746Aurimas Liutikas ContextCompat.getColor(this, android.R.color.white)); 1095b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar touchVH.overlay.setVisibility(View.GONE); 1105b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1115b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 1125b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 1135b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @TargetApi(Build.VERSION_CODES.HONEYCOMB) 1145b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { 1155b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar ItemTouchViewHolder touchVH = (ItemTouchViewHolder) viewHolder; 1165b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) { 117e8e0eae2600001c8b1e9d5a9f0331cf045193746Aurimas Liutikas touchVH.cardView.setCardBackgroundColor( 118e8e0eae2600001c8b1e9d5a9f0331cf045193746Aurimas Liutikas ContextCompat.getColor(this, R.color.card_aquatic)); 1195b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar if (mCustomSwipeEnabled) { 1205b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar // hide it 1215b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar touchVH.overlay.setTranslationX(viewHolder.itemView.getWidth()); 1225b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar touchVH.overlay.setVisibility(View.VISIBLE); 1235b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1245b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1255b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar super.onSelectedChanged(viewHolder, actionState); 1265b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1275b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 1285b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 1295b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @TargetApi(Build.VERSION_CODES.HONEYCOMB) 1305b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public boolean onChildDraw(Canvas c, RecyclerView recyclerView, 1315b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, 1325b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar boolean isCurrentlyActive) { 1335b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar if (!mCustomSwipeEnabled) { 1345b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar return false; 1355b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1365b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar ItemTouchViewHolder touchVH = (ItemTouchViewHolder) viewHolder; 1375b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar final float dir = Math.signum(dX); 1385b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar if (dir == 0) { 1395b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar touchVH.overlay.setTranslationX(-touchVH.overlay.getWidth()); 1405b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } else { 1415b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar final float overlayOffset = dX - dir * viewHolder.itemView.getWidth(); 1425b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar touchVH.overlay.setTranslationX(overlayOffset); 1435b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1445b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar float alpha = (float) (.2 + .8 * Math.abs(dX) / viewHolder.itemView.getWidth()); 1455b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar touchVH.overlay.setAlpha(alpha); 1465b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar return true; 1475b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1485b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 1495b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 1505b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public ItemTouchViewHolder onCreateViewHolder(ViewGroup parent) { 1515b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar final ItemTouchViewHolder vh = super.onCreateViewHolder(parent); 1525b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar vh.actionButton.setText(R.string.swipe); 1535b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar vh.actionButton.setOnTouchListener(new View.OnTouchListener() { 1545b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 1555b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public boolean onTouch(View v, MotionEvent event) { 1565b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) { 1575b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar mItemTouchHelper.startSwipe(vh); 1585b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1595b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar return false; 1605b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1615b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar }); 1625b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar return vh; 1635b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1645b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 1655b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 1665b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public boolean isPointerSwipeEnabled() { 1675b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar return mPointerSwipeEnabled; 1685b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1695b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar 1705b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar @Override 1715b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { 1725b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar return mCallback.makeMovementFlags(0, 1735b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar (mSwipeStartEnabled ? ItemTouchHelper.START : 0) | 1745b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar (mSwipeEndEnabled ? ItemTouchHelper.END : 0)); 1755b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar } 1765b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar} 177