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.view.MotionEvent;
20 import android.view.View;
21 import android.view.ViewGroup;
22 
23 import androidx.core.content.ContextCompat;
24 import androidx.recyclerview.widget.ItemTouchHelper;
25 import androidx.recyclerview.widget.RecyclerView;
26 
27 import com.example.android.supportv7.R;
28 import com.example.android.supportv7.widget.util.ConfigToggle;
29 
30 public class DragAndDropActivity extends ItemTouchHelperActivity {
31 
32     boolean mDragUpEnabled = true;
33     boolean mDragDownEnabled = true;
34     boolean mLongPressDragEnabled = true;
35 
36     @Override
createConfigToggles()37     ConfigToggle[] createConfigToggles() {
38         return new ConfigToggle[]{
39                 new ConfigToggle(this, R.string.drag_up) {
40                     @Override
41                     public boolean isChecked() {
42                         return mDragUpEnabled;
43                     }
44 
45                     @Override
46                     public void onChange(boolean newValue) {
47                         mDragUpEnabled = newValue;
48                     }
49                 },
50                 new ConfigToggle(this, R.string.drag_down) {
51                     @Override
52                     public boolean isChecked() {
53                         return mDragDownEnabled;
54                     }
55 
56                     @Override
57                     public void onChange(boolean newValue) {
58                         mDragDownEnabled = newValue;
59                     }
60                 },
61                 new ConfigToggle(this, R.string.long_press_drag) {
62                     @Override
63                     public boolean isChecked() {
64                         return mLongPressDragEnabled;
65                     }
66 
67                     @Override
68                     public void onChange(boolean newValue) {
69                         mLongPressDragEnabled = newValue;
70                         mAdapter.notifyDataSetChanged();
71                     }
72                 }
73         };
74     }
75 
76     @Override
77     public boolean isLongPressDragEnabled() {
78         return mLongPressDragEnabled;
79     }
80 
81     @Override
82     public void onBind(ItemTouchViewHolder viewHolder) {
83         super.onBind(viewHolder);
84         viewHolder.actionButton.setVisibility(mLongPressDragEnabled ? View.GONE : View.VISIBLE);
85     }
86 
87     @Override
88     public void clearView(RecyclerView.ViewHolder viewHolder) {
89         super.clearView(viewHolder);
90         ItemTouchViewHolder touchVH = (ItemTouchViewHolder) viewHolder;
91         touchVH.cardView.setCardBackgroundColor(
92                 ContextCompat.getColor(this, android.R.color.white));
93         touchVH.overlay.setVisibility(View.GONE);
94     }
95 
96     @Override
97     public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
98         ItemTouchViewHolder touchVH = (ItemTouchViewHolder) viewHolder;
99         if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
100             touchVH.cardView.setCardBackgroundColor(
101                     ContextCompat.getColor(this, R.color.card_aquatic));
102         }
103         super.onSelectedChanged(viewHolder, actionState);
104     }
105 
106     @Override
107     public ItemTouchViewHolder onCreateViewHolder(ViewGroup parent) {
108         final ItemTouchViewHolder vh = super.onCreateViewHolder(parent);
109         vh.actionButton.setText(R.string.drag);
110         vh.actionButton.setOnTouchListener(new View.OnTouchListener() {
111             @Override
112             public boolean onTouch(View v, MotionEvent event) {
113                 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
114                     mItemTouchHelper.startDrag(vh);
115                 }
116                 return false;
117             }
118         });
119         return vh;
120     }
121 
122     @Override
123     public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
124         return mCallback.makeMovementFlags(
125                 (mDragUpEnabled ? ItemTouchHelper.UP : 0) |
126                         (mDragDownEnabled ? ItemTouchHelper.DOWN : 0), 0);
127     }
128 }
129