-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathTrackListRVAdapter.java
More file actions
168 lines (136 loc) · 5.3 KB
/
TrackListRVAdapter.java
File metadata and controls
168 lines (136 loc) · 5.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
package net.osmtracker.activity;
import android.content.Context;
import android.database.Cursor;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.graphics.Color;
import android.util.TypedValue;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import net.osmtracker.R;
import net.osmtracker.db.TracklistAdapter;
import net.osmtracker.db.TrackContentProvider;
public class TrackListRVAdapter extends RecyclerView.Adapter<TrackListRVAdapter.TrackItemVH> {
private static final String TAG = TrackListRVAdapter.class.getSimpleName();
private final TracklistAdapter cursorAdapter;
private final Context context;
private final TrackListRecyclerViewAdapterListener mHandler;
public TrackListRVAdapter(Context context, Cursor cursor,
TrackListRecyclerViewAdapterListener handler) {
this.context = context;
this.cursorAdapter = new TracklistAdapter(context, cursor);
this.mHandler = handler;
}
public TracklistAdapter getCursorAdapter() {
return cursorAdapter;
}
public interface TrackListRecyclerViewAdapterListener {
default void onClick(long trackId) {};
void onCreateContextMenu(ContextMenu contextMenu, View view,
ContextMenu.ContextMenuInfo contextMenuInfo, long trackId);
default void onClick(TrackItemVH item, long trackId) {
onClick(trackId);
}
default void initializeItem(TrackItemVH item, long trackId) {
}
}
/**
* Provide a reference to the type of views
*/
public class TrackItemVH extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnCreateContextMenuListener{
private final TextView vId;
private final TextView vNameOrStartDate;
private final TextView vWps;
private final TextView vTps;
private final TextView vNotesCount;
private final ImageView vStatus;
private final ImageView vUploadStatus;
public TrackItemVH(View view) {
super(view);
vId = (TextView) view.findViewById(R.id.trackmgr_item_id);
vNameOrStartDate = (TextView) view.findViewById(R.id.trackmgr_item_nameordate);
vWps = (TextView) view.findViewById(R.id.trackmgr_item_wps);
vTps = (TextView) view.findViewById(R.id.trackmgr_item_tps);
vNotesCount = view.findViewById(R.id.trackmgr_item_notes_count);
vStatus = (ImageView) view.findViewById(R.id.trackmgr_item_statusicon);
vUploadStatus = (ImageView) view.findViewById(R.id.trackmgr_item_upload_statusicon);
// listeners
view.setOnClickListener(this);
view.setOnCreateContextMenuListener(this);
}
public TextView getvId() {
return vId;
}
public TextView getvNameOrStartDate() {
return vNameOrStartDate;
}
public TextView getvWps() {
return vWps;
}
public TextView getvTps() {
return vTps;
}
public ImageView getvStatus() {
return vStatus;
}
public ImageView getvUploadStatus() {
return vUploadStatus;
}
/**
* This gets called by the child views during a click.
*
* @param v The View that was clicked
*/
@Override
public void onClick(View v) {
long trackId = Long.parseLong(getvId().getText().toString());
mHandler.onClick(this, trackId);
}
public void activate(boolean state) {
if(state)
itemView.setBackgroundColor(Color.RED);
else {
TypedValue outValue = new TypedValue();
itemView
.getContext()
.getTheme()
.resolveAttribute(android.R.attr.selectableItemBackground,
outValue, true);
itemView.setBackgroundResource(outValue.resourceId);
}
}
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
long trackId = Long.parseLong(getvId().getText().toString());
mHandler.onCreateContextMenu(contextMenu, view, contextMenuInfo, trackId);
}
}
@NonNull
@Override
public TrackItemVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new TrackItemVH(LayoutInflater.from(parent.getContext())
.inflate(R.layout.tracklist_item,
parent, false));
}
// Create new views (invoked by the layout manager)
@Override
public void onBindViewHolder(@NonNull TrackItemVH holder, int position) {
// Get element from database at this position and replace the
// contents of the view with that element
// Passing the binding operation to cursor loader
Cursor cursor = cursorAdapter.getCursor();
cursor.moveToPosition(position);
cursorAdapter.bindView(holder.itemView, context, cursor);
mHandler.initializeItem(holder,
cursor.getLong(cursor.getColumnIndex(TrackContentProvider.Schema.COL_ID)));
}
@Override
public int getItemCount() {
return cursorAdapter.getCount();
}
}