-
Notifications
You must be signed in to change notification settings - Fork 17
/
SampleAdapter.java
122 lines (107 loc) · 5.12 KB
/
SampleAdapter.java
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
/*
* Copyright (C) 2015 Thomas Robert Altstidl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tr4android.recyclerviewslideitemsample;
import android.content.Context;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.tr4android.recyclerviewslideitem.SwipeAdapter;
import com.tr4android.recyclerviewslideitem.SwipeConfiguration;
import java.util.ArrayList;
public class SampleAdapter extends SwipeAdapter implements View.OnClickListener {
ArrayList<String> mDataset;
int[] colors = new int[]{R.color.color_red, R.color.color_pink, R.color.color_purple, R.color.color_deep_purple, R.color.color_indigo, R.color.color_blue, R.color.color_light_blue, R.color.color_cyan, R.color.color_teal, R.color.color_green, R.color.color_light_green, R.color.color_lime, R.color.color_yellow, R.color.color_amber, R.color.color_orange, R.color.color_deep_orange, R.color.color_brown, R.color.color_grey, R.color.color_blue_grey};
private Context mContext;
private RecyclerView mRecyclerView;
public SampleAdapter(Context context, RecyclerView recyclerView) {
mContext = context;
mRecyclerView = recyclerView;
// create dummy dataset
mDataset = new ArrayList<>();
for (int i = 0; i < 25; i++) {
mDataset.add("person" + String.valueOf(i + 1) + "@sample.com");
}
}
public class SampleViewHolder extends RecyclerView.ViewHolder {
LinearLayout contentView;
View avatarView;
TextView textView;
public SampleViewHolder(View view) {
super(view);
contentView = (LinearLayout) view.findViewById(R.id.contentView);
avatarView = view.findViewById(R.id.avatarView);
textView = (TextView) view.findViewById(R.id.textView);
contentView.setOnClickListener(SampleAdapter.this);
}
}
@Override
public RecyclerView.ViewHolder onCreateSwipeViewHolder(ViewGroup parent, int i) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_sample, parent, true);
return new SampleViewHolder(v);
}
@Override
public void onBindSwipeViewHolder(RecyclerView.ViewHolder swipeViewHolder, int i) {
SampleViewHolder sampleViewHolder = (SampleViewHolder) swipeViewHolder;
ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(mContext.getResources().getColor(colors[((int) (Math.random() * (colors.length - 1)))]));
sampleViewHolder.avatarView.setBackgroundDrawable(drawable);
sampleViewHolder.textView.setText(mDataset.get(i));
}
@Override
public SwipeConfiguration onCreateSwipeConfiguration(Context context, int position) {
return new SwipeConfiguration.Builder(context)
.setLeftBackgroundColorResource(R.color.color_delete)
.setRightBackgroundColorResource(R.color.color_mark)
.setDrawableResource(R.drawable.ic_delete_white_24dp)
.setRightDrawableResource(R.drawable.ic_done_white_24dp)
.setLeftUndoable(true)
.setLeftUndoDescription(R.string.action_deleted)
.setDescriptionTextColorResource(android.R.color.white)
.setLeftSwipeBehaviour(SwipeConfiguration.SwipeBehaviour.NORMAL_SWIPE)
.setRightSwipeBehaviour(SwipeConfiguration.SwipeBehaviour.RESTRICTED_SWIPE)
.build();
}
@Override
public void onSwipe(int position, int direction) {
if (direction == SWIPE_LEFT) {
mDataset.remove(position);
notifyItemRemoved(position);
Toast toast = Toast.makeText(mContext, "Deleted item at position " + position, Toast.LENGTH_SHORT);
toast.show();
} else {
Toast toast = Toast.makeText(mContext, "Marked item as read at position " + position, Toast.LENGTH_SHORT);
toast.show();
}
}
@Override
public void onClick(View view) {
// We need to get the parent of the parent to actually have the proper view
int position = mRecyclerView.getChildAdapterPosition((View) view.getParent().getParent());
Toast toast = Toast.makeText(mContext, "Clicked item at position " + position, Toast.LENGTH_SHORT);
toast.show();
}
@Override
public int getItemCount() {
return mDataset.size();
}
}