Skip to content

Commit 076365d

Browse files
committed
Make use of standard RecyclerView Holder optimizations.
1 parent 1bf85d0 commit 076365d

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

app/src/main/java/com/firebase/uidemo/database/ChatActivity.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -226,49 +226,50 @@ public String getText() {
226226
}
227227

228228
public static class ChatHolder extends RecyclerView.ViewHolder {
229-
View mView;
229+
private final TextView mNameField;
230+
private final TextView mTextField;
231+
private final FrameLayout mLeftArrow;
232+
private final FrameLayout mRightArrow;
233+
private final RelativeLayout mMessageContainer;
234+
private final LinearLayout mMessage;
230235

231236
public ChatHolder(View itemView) {
232237
super(itemView);
233-
mView = itemView;
238+
mNameField = (TextView) itemView.findViewById(R.id.name_text);
239+
mTextField = (TextView) itemView.findViewById(R.id.message_text);
240+
mLeftArrow = (FrameLayout) itemView.findViewById(R.id.left_arrow);
241+
mRightArrow = (FrameLayout) itemView.findViewById(R.id.right_arrow);
242+
mMessageContainer = (RelativeLayout) itemView.findViewById(R.id.message_container);
243+
mMessage = (LinearLayout) itemView.findViewById(R.id.message);
234244
}
235245

236-
public void setIsSender(Boolean isSender) {
237-
FrameLayout left_arrow = (FrameLayout) mView.findViewById(R.id.left_arrow);
238-
FrameLayout right_arrow = (FrameLayout) mView.findViewById(R.id.right_arrow);
239-
RelativeLayout messageContainer = (RelativeLayout) mView.findViewById(R.id.message_container);
240-
LinearLayout message = (LinearLayout) mView.findViewById(R.id.message);
241-
242-
int color;
246+
public void setIsSender(boolean isSender) {
247+
final int color;
243248
if (isSender) {
244-
color = ContextCompat.getColor(mView.getContext(), R.color.material_green_300);
245-
246-
left_arrow.setVisibility(View.GONE);
247-
right_arrow.setVisibility(View.VISIBLE);
248-
messageContainer.setGravity(Gravity.END);
249+
color = ContextCompat.getColor(itemView.getContext(), R.color.material_green_300);
250+
mLeftArrow.setVisibility(View.GONE);
251+
mRightArrow.setVisibility(View.VISIBLE);
252+
mMessageContainer.setGravity(Gravity.END);
249253
} else {
250-
color = ContextCompat.getColor(mView.getContext(), R.color.material_gray_300);
251-
252-
left_arrow.setVisibility(View.VISIBLE);
253-
right_arrow.setVisibility(View.GONE);
254-
messageContainer.setGravity(Gravity.START);
254+
color = ContextCompat.getColor(itemView.getContext(), R.color.material_gray_300);
255+
mLeftArrow.setVisibility(View.VISIBLE);
256+
mRightArrow.setVisibility(View.GONE);
257+
mMessageContainer.setGravity(Gravity.START);
255258
}
256259

257-
((GradientDrawable) message.getBackground()).setColor(color);
258-
((RotateDrawable) left_arrow.getBackground()).getDrawable()
260+
((GradientDrawable) mMessage.getBackground()).setColor(color);
261+
((RotateDrawable) mLeftArrow.getBackground()).getDrawable()
259262
.setColorFilter(color, PorterDuff.Mode.SRC);
260-
((RotateDrawable) right_arrow.getBackground()).getDrawable()
263+
((RotateDrawable) mRightArrow.getBackground()).getDrawable()
261264
.setColorFilter(color, PorterDuff.Mode.SRC);
262265
}
263266

264267
public void setName(String name) {
265-
TextView field = (TextView) mView.findViewById(R.id.name_text);
266-
field.setText(name);
268+
mNameField.setText(name);
267269
}
268270

269271
public void setText(String text) {
270-
TextView field = (TextView) mView.findViewById(R.id.message_text);
271-
field.setText(text);
272+
mTextField.setText(text);
272273
}
273274
}
274275
}

database/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,21 +228,21 @@ We can wrap that in a ViewHolder with:
228228

229229
```java
230230
public static class ChatHolder extends RecyclerView.ViewHolder {
231-
View mView;
231+
private final TextView mNameField;
232+
private final TextView mTextField;
232233

233234
public ChatHolder(View itemView) {
234235
super(itemView);
235-
mView = itemView;
236+
mNameField = (TextView) itemView.findViewById(android.R.id.text1);
237+
mTextField = (TextView) itemView.findViewById(android.R.id.text2);
236238
}
237239

238240
public void setName(String name) {
239-
TextView field = (TextView) mView.findViewById(android.R.id.text1);
240-
field.setText(name);
241+
mNameField.setText(name);
241242
}
242243

243244
public void setText(String text) {
244-
TextView field = (TextView) mView.findViewById(android.R.id.text2);
245-
field.setText(text);
245+
mTextField.setText(text);
246246
}
247247
}
248248
```

0 commit comments

Comments
 (0)