Skip to content

Commit 92caa19

Browse files
leticiarossidrchen
authored andcommittedAug 4, 2022
[Divider] Fixed lastItemDecorated not being applied to horizontal orientation. Also updated the demo to include both orientations.
Resolves #2802 PiperOrigin-RevId: 461247080 (cherry picked from commit 6eeb95f)
1 parent a8ae9c4 commit 92caa19

File tree

5 files changed

+67
-22
lines changed

5 files changed

+67
-22
lines changed
 

‎catalog/java/io/material/catalog/divider/DividerItemDecorationDemoFragment.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
/** Demo of the MaterialDividerItemDecoration. */
3434
public class DividerItemDecorationDemoFragment extends DemoFragment {
3535

36-
private RecyclerView recyclerView;
37-
private DividerAdapter adapter;
38-
private RecyclerView.LayoutManager layoutManager;
39-
4036
@Nullable
4137
@Override
4238
public View onCreateDemoView(
@@ -47,24 +43,34 @@ public View onCreateDemoView(
4743
layoutInflater.inflate(
4844
R.layout.cat_divider_recyclerview_fragment, viewGroup, /* attachToRoot */ false);
4945

50-
recyclerView = view.findViewById(R.id.divider_recyclerview);
51-
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
46+
RecyclerView recyclerViewHorizontal = view.findViewById(R.id.divider_recyclerview_horizontal);
47+
RecyclerView recyclerViewVertical = view.findViewById(R.id.divider_recyclerview_vertical);
48+
49+
setUpDividers(recyclerViewHorizontal, LinearLayoutManager.HORIZONTAL);
50+
setUpDividers(recyclerViewVertical, LinearLayoutManager.VERTICAL);
51+
52+
return view;
53+
}
54+
55+
private void setUpDividers(@NonNull RecyclerView recyclerView, int orientation) {
56+
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(),
57+
orientation, false);
5258
recyclerView.setLayoutManager(layoutManager);
5359

5460
MaterialDividerItemDecoration divider =
55-
new MaterialDividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL);
61+
new MaterialDividerItemDecoration(getContext(), orientation);
5662
recyclerView.addItemDecoration(divider);
5763

58-
adapter = new DividerAdapter();
64+
DividerAdapter adapter = new DividerAdapter();
5965
recyclerView.setAdapter(adapter);
60-
61-
return view;
6266
}
6367

6468
/** A RecyclerView adapter. */
6569
private static final class DividerAdapter
6670
extends RecyclerView.Adapter<DividerAdapter.MyViewHolder> {
6771

72+
private static final int ITEM_COUNT = 20;
73+
6874
/** Provide a reference to the views for each data item. */
6975
private static class MyViewHolder extends RecyclerView.ViewHolder {
7076

@@ -95,7 +101,7 @@ public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
95101

96102
@Override
97103
public int getItemCount() {
98-
return 30;
104+
return ITEM_COUNT;
99105
}
100106
}
101107
}

‎catalog/java/io/material/catalog/divider/res/layout/cat_divider_recyclerview_fragment.xml

+36-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,40 @@
1515
limitations under the License.
1616
-->
1717

18-
<androidx.recyclerview.widget.RecyclerView
19-
xmlns:android="http://schemas.android.com/apk/res/android"
20-
android:id="@+id/divider_recyclerview"
21-
android:layout_height="wrap_content"
18+
<ScrollView
19+
xmlns:android="http://schemas.android.com/apk/res/android"
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent">
22+
<LinearLayout
2223
android:layout_width="match_parent"
23-
android:layout_margin="16dp"/>
24+
android:layout_height="wrap_content"
25+
android:layout_margin="16dp"
26+
android:orientation="vertical">
27+
28+
<!-- Horizontal orientation dividers. -->
29+
<TextView
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:layout_marginBottom="16dp"
33+
android:textAppearance="?attr/textAppearanceTitleLarge"
34+
android:text="@string/cat_divider_item_decoration_horizontal"/>
35+
<androidx.recyclerview.widget.RecyclerView
36+
android:id="@+id/divider_recyclerview_horizontal"
37+
android:layout_marginBottom="16dp"
38+
android:layout_height="wrap_content"
39+
android:layout_width="match_parent"/>
40+
41+
<!-- Vertical orientation dividers. -->
42+
<TextView
43+
android:layout_width="wrap_content"
44+
android:layout_height="wrap_content"
45+
android:layout_marginBottom="16dp"
46+
android:textAppearance="?attr/textAppearanceTitleLarge"
47+
android:text="@string/cat_divider_item_decoration_vertical"/>
48+
<androidx.recyclerview.widget.RecyclerView
49+
android:id="@+id/divider_recyclerview_vertical"
50+
android:layout_height="wrap_content"
51+
android:layout_width="match_parent"
52+
android:layout_marginBottom="48dp"/>
53+
</LinearLayout>
54+
</ScrollView>

‎catalog/java/io/material/catalog/divider/res/layout/cat_divider_recyclerview_item.xml

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
<TextView
1919
xmlns:android="http://schemas.android.com/apk/res/android"
20-
android:layout_width="match_parent"
20+
android:layout_width="wrap_content"
2121
android:layout_height="wrap_content"
22-
android:layout_marginTop="8dp"
23-
android:layout_marginBottom="8dp"
22+
android:layout_margin="8dp"
2423
android:textAppearance="?attr/textAppearanceSubtitle1"/>

‎catalog/java/io/material/catalog/divider/res/values/strings.xml

+8
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,12 @@
6666
description="Label of the item in the MaterialDividerItemDecoration demo [CHAR_LIMIT=20]">
6767
Item %1$d
6868
</string>
69+
<string name="cat_divider_item_decoration_horizontal"
70+
description="Subtitle label for dividers with horizontal orientation [CHAR_LIMIT=NONE]">
71+
Layout manager with horizontal orientation
72+
</string>
73+
<string name="cat_divider_item_decoration_vertical"
74+
description="Subtitle label for dividers with vertical orientation [CHAR_LIMIT=NONE]">
75+
Layout manager with vertical orientation
76+
</string>
6977
</resources>

‎lib/java/com/google/android/material/divider/MaterialDividerItemDecoration.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,11 @@ private void drawForHorizontalOrientation(@NonNull Canvas canvas, @NonNull Recyc
352352
bottom -= insetEnd;
353353

354354
int childCount = parent.getChildCount();
355-
for (int i = 0; i < childCount; i++) {
355+
int dividerCount = lastItemDecorated ? childCount : childCount - 1;
356+
for (int i = 0; i < dividerCount; i++) {
356357
View child = parent.getChildAt(i);
357-
parent.getLayoutManager().getDecoratedBoundsWithMargins(child, tempRect);
358-
// Take into consideration any translationY added to the view.
358+
parent.getDecoratedBoundsWithMargins(child, tempRect);
359+
// Take into consideration any translationX added to the view.
359360
int right = tempRect.right + Math.round(child.getTranslationX());
360361
int left = right - dividerDrawable.getIntrinsicWidth() - thickness;
361362
dividerDrawable.setBounds(left, top, right, bottom);

0 commit comments

Comments
 (0)
Please sign in to comment.