Skip to content

Commit

Permalink
Fix possible duplicate views in recycler view items
Browse files Browse the repository at this point in the history
  • Loading branch information
therealsujitk committed Apr 7, 2024
1 parent 8b8406d commit 7047ae3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public void setCourseTitle(String courseTitle, boolean theory, boolean lab, bool
ChipGroup courseTypes = this.courseItem.findViewById(R.id.chip_group_course_types);

title.setText(courseTitle);
courseTypes.removeAllViews();

if (theory) {
Chip chip = new Chip(this.courseItem.getContext());
Expand Down Expand Up @@ -159,9 +160,7 @@ public void setCourseItem(Course.AllData courseItem) {
TextView faculty = this.courseItem.findViewById(R.id.text_view_faculty);
TextView venue = this.courseItem.findViewById(R.id.text_view_venue);
TextView attendanceText = this.courseItem.findViewById(R.id.text_view_attendance);

ChipGroup slots = this.courseItem.findViewById(R.id.chip_group_slots);

ProgressBar attendanceProgress = this.courseItem.findViewById(R.id.progress_bar_attendance);

faculty.setText(Html.fromHtml(this.courseItem.getContext().getString(R.string.faculty, courseItem.faculty), Html.FROM_HTML_MODE_LEGACY));
Expand All @@ -175,6 +174,8 @@ public void setCourseItem(Course.AllData courseItem) {
chipIconResource = R.drawable.ic_project;
}

slots.removeAllViews();

for (int i = 0; i < courseItem.slots.size(); ++i) {
Chip slot = new Chip(this.courseItem.getContext());
slot.setChipIconResource(chipIconResource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newStat
}

private void addChips() {
this.courseTypes.removeAllViews();
List<String> courseTypes = new ArrayList<>();

if (this.marks.containsKey("theory")) {
Expand All @@ -121,6 +120,8 @@ private void addChips() {
if (this.marks.containsKey("project")) {
courseTypes.add("project");
}

this.courseTypes.removeAllViews();

for (int i = 0; i < this.marks.size(); ++i) {
String courseType = courseTypes.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ public ViewHolder(@NonNull View itemView) {
}

public void setMarksItem(Mark.AllData marksItem) {
LinearLayout markDetails = this.marksItem.findViewById(R.id.linear_layout_details);
AppCompatTextView markTitle = this.marksItem.findViewById(R.id.text_view_title);
AppCompatTextView averageText = this.marksItem.findViewById(R.id.text_view_average);
AppCompatTextView statusText = this.marksItem.findViewById(R.id.text_view_status);
AppCompatTextView scoreText = this.marksItem.findViewById(R.id.text_view_score);
ProgressBar scoreProgress = this.marksItem.findViewById(R.id.progress_bar_score);
AppCompatTextView markType = this.marksItem.findViewById(R.id.text_view_mark_type);
Chip courseType = this.marksItem.findViewById(R.id.chip_course_type);

markTitle.setText(marksItem.title);

Expand Down Expand Up @@ -115,21 +117,19 @@ public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int
}

if (marksItem.average != null) {
markDetails.addView(this.createTextView(this.marksItem.getContext().getString(R.string.average, marksItem.average)));
averageText.setText(Html.fromHtml(this.marksItem.getContext().getString(R.string.average, marksItem.average), Html.FROM_HTML_MODE_LEGACY));
averageText.setVisibility(View.VISIBLE);
} else {
averageText.setVisibility(View.GONE);
}

if (marksItem.status != null) {
markDetails.addView(this.createTextView(this.marksItem.getContext().getString(R.string.status, marksItem.status)));
statusText.setText(Html.fromHtml(this.marksItem.getContext().getString(R.string.status, marksItem.status), Html.FROM_HTML_MODE_LEGACY));
statusText.setVisibility(View.VISIBLE);
} else {
statusText.setVisibility(View.GONE);
}

Chip courseType = new Chip(this.marksItem.getContext());
courseType.setClickable(false);
courseType.setFocusable(false);
courseType.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));

if (marksItem.courseType.equals("lab")) {
courseType.setChipIconResource(R.drawable.ic_lab);
courseType.setText(R.string.lab);
Expand All @@ -140,16 +140,6 @@ public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int
courseType.setChipIconResource(R.drawable.ic_theory);
courseType.setText(R.string.theory);
}

markDetails.addView(courseType);
}

AppCompatTextView createTextView(String text) {
AppCompatTextView textView = new AppCompatTextView(this.marksItem.getContext());
textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));
textView.setTextSize(16);

return textView;
}
}
}
26 changes: 25 additions & 1 deletion app/src/main/res/layout/layout_item_marks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
android:paddingBottom="20dp">

<LinearLayout
android:id="@+id/linear_layout_details"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
Expand All @@ -32,6 +31,31 @@
android:textColor="?attr/colorSecondary"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/text_view_average"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:textColor="?attr/colorSecondary"
android:textSize="16sp"
android:visibility="gone" />

<TextView
android:id="@+id/text_view_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:textColor="?attr/colorSecondary"
android:textSize="16sp"
android:visibility="gone" />

<com.google.android.material.chip.Chip
android:id="@+id/chip_course_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false" />
</LinearLayout>

<LinearLayout
Expand Down

0 comments on commit 7047ae3

Please sign in to comment.