Skip to content

Make use of standard RecyclerView Holder optimizations. #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions app/src/main/java/com/firebase/uidemo/database/ChatActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,49 +226,50 @@ public String getText() {
}

public static class ChatHolder extends RecyclerView.ViewHolder {
View mView;
private final TextView mNameField;
private final TextView mTextField;
private final FrameLayout mLeftArrow;
private final FrameLayout mRightArrow;
private final RelativeLayout mMessageContainer;
private final LinearLayout mMessage;

public ChatHolder(View itemView) {
super(itemView);
mView = itemView;
mNameField = (TextView) itemView.findViewById(R.id.name_text);
mTextField = (TextView) itemView.findViewById(R.id.message_text);
mLeftArrow = (FrameLayout) itemView.findViewById(R.id.left_arrow);
mRightArrow = (FrameLayout) itemView.findViewById(R.id.right_arrow);
mMessageContainer = (RelativeLayout) itemView.findViewById(R.id.message_container);
mMessage = (LinearLayout) itemView.findViewById(R.id.message);
}

public void setIsSender(Boolean isSender) {
FrameLayout left_arrow = (FrameLayout) mView.findViewById(R.id.left_arrow);
FrameLayout right_arrow = (FrameLayout) mView.findViewById(R.id.right_arrow);
RelativeLayout messageContainer = (RelativeLayout) mView.findViewById(R.id.message_container);
LinearLayout message = (LinearLayout) mView.findViewById(R.id.message);

int color;
public void setIsSender(boolean isSender) {
final int color;
if (isSender) {
color = ContextCompat.getColor(mView.getContext(), R.color.material_green_300);

left_arrow.setVisibility(View.GONE);
right_arrow.setVisibility(View.VISIBLE);
messageContainer.setGravity(Gravity.END);
color = ContextCompat.getColor(itemView.getContext(), R.color.material_green_300);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're feeling cache-y, might as well cache these two colors on ViewHolder creation (or even at a higher level)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considered that, but those could actually theoretically change when the configuration changes as well. If we want to assume they don't, then they're cachable as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a very simple sample app let's assume they don't.

mLeftArrow.setVisibility(View.GONE);
mRightArrow.setVisibility(View.VISIBLE);
mMessageContainer.setGravity(Gravity.END);
} else {
color = ContextCompat.getColor(mView.getContext(), R.color.material_gray_300);

left_arrow.setVisibility(View.VISIBLE);
right_arrow.setVisibility(View.GONE);
messageContainer.setGravity(Gravity.START);
color = ContextCompat.getColor(itemView.getContext(), R.color.material_gray_300);
mLeftArrow.setVisibility(View.VISIBLE);
mRightArrow.setVisibility(View.GONE);
mMessageContainer.setGravity(Gravity.START);
}

((GradientDrawable) message.getBackground()).setColor(color);
((RotateDrawable) left_arrow.getBackground()).getDrawable()
((GradientDrawable) mMessage.getBackground()).setColor(color);
((RotateDrawable) mLeftArrow.getBackground()).getDrawable()
.setColorFilter(color, PorterDuff.Mode.SRC);
((RotateDrawable) right_arrow.getBackground()).getDrawable()
((RotateDrawable) mRightArrow.getBackground()).getDrawable()
.setColorFilter(color, PorterDuff.Mode.SRC);
}

public void setName(String name) {
TextView field = (TextView) mView.findViewById(R.id.name_text);
field.setText(name);
mNameField.setText(name);
}

public void setText(String text) {
TextView field = (TextView) mView.findViewById(R.id.message_text);
field.setText(text);
mTextField.setText(text);
}
}
}
12 changes: 6 additions & 6 deletions database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,21 @@ We can wrap that in a ViewHolder with:

```java
public static class ChatHolder extends RecyclerView.ViewHolder {
View mView;
private final TextView mNameField;
private final TextView mTextField;

public ChatHolder(View itemView) {
super(itemView);
mView = itemView;
mNameField = (TextView) itemView.findViewById(android.R.id.text1);
mTextField = (TextView) itemView.findViewById(android.R.id.text2);
}

public void setName(String name) {
TextView field = (TextView) mView.findViewById(android.R.id.text1);
field.setText(name);
mNameField.setText(name);
}

public void setText(String text) {
TextView field = (TextView) mView.findViewById(android.R.id.text2);
field.setText(text);
mTextField.setText(text);
}
}
```
Expand Down