-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreenAdapter.java
145 lines (130 loc) · 5.86 KB
/
GreenAdapter.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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.example.android.recyclerview;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* We couldn't come up with a good name for this class. Then, we realized
* that this lesson is about RecyclerView.
*
* RecyclerView... Recycling... Saving the planet? Being green? Anyone?
* #crickets
*
* Avoid unnecessary garbage collection by using RecyclerView and ViewHolders.
*
* If you don't like our puns, we named this Adapter GreenAdapter because its
* contents are green.
*/
// COMPLETED (4) From GreenAdapter, extend RecyclerView.Adapter<NumberViewHolder>
public class GreenAdapter extends RecyclerView.Adapter<GreenAdapter.NumberViewHolder> {
private static final String TAG = GreenAdapter.class.getSimpleName();
// COMPLETED (1) Add a private int variable called mNumberItems
private int mNumberItems;
// COMPLETED (2) Create a constructor for GreenAdapter that accepts an int as a parameter for numberOfItems
// COMPLETED (3) Store the numberOfItems parameter in mNumberItems
/**
* Constructor for GreenAdapter that accepts a number of items to display and the specification
* for the ListItemClickListener.
*
* @param numberOfItems Number of items to display in list
*/
public GreenAdapter(int numberOfItems) {
mNumberItems = numberOfItems;
}
// COMPLETED (5) Override the onCreateViewHolder method
// COMPLETED (6) Create and return a new NumberViewHolder within this method
/**
*
* This gets called when each new ViewHolder is created. This happens when the RecyclerView
* is laid out. Enough ViewHolders will be created to fill the screen and allow for scrolling.
*
* @param viewGroup The ViewGroup that these ViewHolders are contained within.
* @param viewType If your RecyclerView has more than one type of item (which ours doesn't) you
* can use this viewType integer to provide a different layout. See
* {@link android.support.v7.widget.RecyclerView.Adapter#getItemViewType(int)}
* for more details.
* @return A new NumberViewHolder that holds the View for each list item
*/
@Override
public NumberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.number_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
NumberViewHolder viewHolder = new NumberViewHolder(view);
return viewHolder;
}
// COMPLETED (7) Override onBindViewHolder
// COMPLETED (8) Within onBindViewHolder, call holder.bind and pass in the position
/**
* OnBindViewHolder is called by the RecyclerView to display the data at the specified
* position. In this method, we update the contents of the ViewHolder to display the correct
* indices in the list for this particular position, using the "position" argument that is conveniently
* passed into us.
*
* @param holder The ViewHolder which should be updated to represent the contents of the
* item at the given position in the data set.
* @param position The position of the item within the adapter's data set.
*/
@Override
public void onBindViewHolder(NumberViewHolder holder, int position) {
Log.d(TAG, "#" + position);
holder.bind(position);
}
// COMPLETED (9) Override getItemCount and return the number of items to display
/**
* This method simply returns the number of items to display. It is used behind the scenes
* to help layout our Views and for animations.
*
* @return The number of items available in our forecast
*/
@Override
public int getItemCount() {
return mNumberItems;
}
/**
* Cache of the children views for a list item.
*/
class NumberViewHolder extends RecyclerView.ViewHolder {
// Will display the position in the list, ie 0 through getItemCount() - 1
TextView listItemNumberView;
/**
* Constructor for our ViewHolder. Within this constructor, we get a reference to our
* TextViews and set an onClickListener to listen for clicks. Those will be handled in the
* onClick method below.
* @param itemView The View that you inflated in
* {@link GreenAdapter#onCreateViewHolder(ViewGroup, int)}
*/
public NumberViewHolder(View itemView) {
super(itemView);
listItemNumberView = (TextView) itemView.findViewById(R.id.tv_item_number);
}
/**
* A method we wrote for convenience. This method will take an integer as input and
* use that integer to display the appropriate text within a list item.
* @param listIndex Position of the item in the list
*/
void bind(int listIndex) {
listItemNumberView.setText(String.valueOf(listIndex));
}
}
}