-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathLongClickDeleteTextFlowActivity.java
More file actions
189 lines (165 loc) · 8.3 KB
/
LongClickDeleteTextFlowActivity.java
File metadata and controls
189 lines (165 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.single.flowlayout;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView;
import android.widget.Toast;
import com.library.flowlayout.FlowLayoutManager;
import com.library.flowlayout.SpaceItemDecoration;
import java.util.ArrayList;
import java.util.List;
import static com.single.flowlayout.R.id.flow;
public class LongClickDeleteTextFlowActivity extends AppCompatActivity {
private static final String TAG = TextFlowActivity.class.getSimpleName();
private List<ShowItem> list = new ArrayList<>();
private FlowAdapter flowAdapter;
private boolean touchRv;
RecyclerView recyclerView;
private boolean deleteShowText = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flow_layout);
recyclerView = (RecyclerView) findViewById(flow);
FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
recyclerView.addItemDecoration(new SpaceItemDecoration(dp2px(10)));
recyclerView.setLayoutManager(flowLayoutManager);
list = DataConfig.getItems();
recyclerView.setAdapter(flowAdapter = new FlowAdapter(list));
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
touchRv = true;
} else {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (touchRv) {
flowAdapter.refreshSelect();
touchRv = false;
}
}
}
return true;
}
});
}
class FlowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ShowItem> list;
private int selectPosition = -1;
public FlowAdapter(List<ShowItem> list) {
this.list = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyHolder(View.inflate(LongClickDeleteTextFlowActivity.this, R.layout.flow_item, null));
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
Log.d(TAG, "position:" + position);
Log.d(TAG, "list.get(position).des:" + list.get(position).des);
final TextView textView = ((MyHolder) holder).text;
textView.setBackgroundDrawable(list.get(position).color);
textView.setText(list.get(position).des);
textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
final ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();
final int width = textView.getMeasuredWidth();
final int height = textView.getMeasuredHeight();
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (position != selectPosition) {
if (selectPosition == -1) {
if (!list.get(position).isSelect) {
textView.setBackgroundDrawable(deleteShowText ? getBack() : new DeleteDrawable(LongClickDeleteTextFlowActivity.this));
textView.setText(deleteShowText ? "删除" : null);
layoutParams.width = width;
layoutParams.height = height;
textView.setLayoutParams(layoutParams);
list.get(position).isSelect = true;
selectPosition = position;
}
}
}
touchRv = false;
return true;
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "position:" + position);
Log.d(TAG, "selectPosition:" + selectPosition);
if (position == selectPosition) {
RecyclerView.ViewHolder viewHolderForAdapterPosition = recyclerView.findViewHolderForAdapterPosition(selectPosition);
TextView selectTv = ((MyHolder) viewHolderForAdapterPosition).text;
ViewGroup.LayoutParams lp = selectTv.getLayoutParams();
lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(lp);
list.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, list.size() - position);
selectPosition = -1;
} else {
if (selectPosition != -1) {
RecyclerView.ViewHolder viewHolderForAdapterPosition = recyclerView.findViewHolderForAdapterPosition(selectPosition);
TextView selectTv = ((MyHolder) viewHolderForAdapterPosition).text;
selectTv.setBackgroundDrawable(list.get(selectPosition).color);
selectTv.setText(list.get(selectPosition).des);
list.get(selectPosition).isSelect = false;
selectPosition = -1;
} else {
//做你的跳转
Toast.makeText(LongClickDeleteTextFlowActivity.this, list.get(position).des, Toast.LENGTH_SHORT).show();
}
}
touchRv = false;
}
});
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public void refreshSelect() {
if (selectPosition != -1) {
RecyclerView.ViewHolder viewHolderForAdapterPosition = recyclerView.findViewHolderForAdapterPosition(selectPosition);
TextView selectTv = ((MyHolder) viewHolderForAdapterPosition).text;
selectTv.setBackgroundDrawable(list.get(selectPosition).color);
selectTv.setText(list.get(selectPosition).des);
list.get(selectPosition).isSelect = false;
selectPosition = -1;
}
}
class MyHolder extends RecyclerView.ViewHolder {
private TextView text;
public MyHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.flow_text);
}
}
}
private Drawable getBack() {
GradientDrawable drawable = new GradientDrawable();
drawable.setCornerRadius(8);
drawable.setColor(Color.parseColor("#ff0000"));
return drawable;
}
private int dp2px(float value) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics());
}
}