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
|
package com.zeapo.pwdstore.utils;
import android.app.Activity;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.zeapo.pwdstore.R;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;
public abstract class EntryRecyclerAdapter extends RecyclerView.Adapter<EntryRecyclerAdapter.ViewHolder> {
private final Activity activity;
private final ArrayList<PasswordItem> values;
final Set<Integer> selectedItems = new TreeSet<>();
EntryRecyclerAdapter(Activity activity, ArrayList<PasswordItem> values) {
this.activity = activity;
this.values = values;
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return values.size();
}
public ArrayList<PasswordItem> getValues() {
return this.values;
}
public void clear() {
this.values.clear();
this.notifyDataSetChanged();
}
public void addAll(ArrayList<PasswordItem> list) {
this.values.addAll(list);
this.notifyDataSetChanged();
}
public void add(PasswordItem item) {
this.values.add(item);
this.notifyItemInserted(getItemCount());
}
void toggleSelection(int position) {
if (!selectedItems.remove(position)) {
selectedItems.add(position);
}
}
// use this after an item is removed to update the positions of items in set
// that followed the removed position
public void updateSelectedItems(int position, Set<Integer> selectedItems) {
Set<Integer> temp = new TreeSet<>();
for (int selected : selectedItems) {
if (selected > position) {
temp.add(selected - 1);
} else {
temp.add(selected);
}
}
selectedItems.clear();
selectedItems.addAll(temp);
}
public void remove(int position) {
this.values.remove(position);
this.notifyItemRemoved(position);
// keep selectedItems updated so we know what to notifyItemChanged
// (instead of just using notifyDataSetChanged)
updateSelectedItems(position, selectedItems);
}
@NonNull
protected View.OnLongClickListener getOnLongClickListener(ViewHolder holder, PasswordItem pass) {
return new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return false;
}
};
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final PasswordItem pass = getValues().get(position);
holder.name.setText(pass.toString());
if (pass.getType() == PasswordItem.TYPE_CATEGORY) {
holder.typeImage.setImageResource(R.drawable.ic_folder_grey600_24dp);
holder.name.setText(pass.toString() + "/");
} else {
holder.typeImage.setImageResource(R.drawable.ic_action_secure);
holder.name.setText(pass.toString());
}
holder.type.setText(pass.getFullPathToParent());
holder.view.setOnClickListener(getOnClickListener(holder, pass));
holder.view.setOnLongClickListener(getOnLongClickListener(holder, pass));
// after removal, everything is rebound for some reason; views are shuffled?
boolean selected = selectedItems.contains(position);
holder.view.setSelected(selected);
if (selected) {
holder.itemView.setBackgroundResource(R.color.deep_orange_200);
holder.type.setTextColor(Color.BLACK);
} else {
holder.itemView.setBackgroundResource(Color.alpha(1));
holder.type.setTextColor(ContextCompat.getColor(activity, R.color.grey_500));
}
}
@NonNull
protected abstract View.OnClickListener getOnClickListener(ViewHolder holder, PasswordItem pass);
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public final View view;
public final TextView name;
final TextView type;
final ImageView typeImage;
ViewHolder(View v) {
super(v);
view = v;
name = (TextView) view.findViewById(R.id.label);
type = (TextView) view.findViewById(R.id.type);
typeImage = (ImageView) view.findViewById(R.id.type_image);
}
}
// Create new views (invoked by the layout manager)
@Override
public PasswordRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.password_row_layout, parent, false);
return new ViewHolder(v);
}
}
|