From 92caa194b8e99de232cee00a696f05dff28f78dc Mon Sep 17 00:00:00 2001 From: leticiars Date: Fri, 15 Jul 2022 21:59:37 +0000 Subject: [PATCH] [Divider] Fixed lastItemDecorated not being applied to horizontal orientation. Also updated the demo to include both orientations. Resolves https://github.com/material-components/material-components-android/issues/2802 PiperOrigin-RevId: 461247080 (cherry picked from commit 6eeb95f9717d58a842dc4c20aeda8048dc55077d) --- .../DividerItemDecorationDemoFragment.java | 28 ++++++++----- .../cat_divider_recyclerview_fragment.xml | 41 ++++++++++++++++--- .../layout/cat_divider_recyclerview_item.xml | 5 +-- .../catalog/divider/res/values/strings.xml | 8 ++++ .../MaterialDividerItemDecoration.java | 7 ++-- 5 files changed, 67 insertions(+), 22 deletions(-) diff --git a/catalog/java/io/material/catalog/divider/DividerItemDecorationDemoFragment.java b/catalog/java/io/material/catalog/divider/DividerItemDecorationDemoFragment.java index 3a8a9cda1af..7f6702ad9c0 100644 --- a/catalog/java/io/material/catalog/divider/DividerItemDecorationDemoFragment.java +++ b/catalog/java/io/material/catalog/divider/DividerItemDecorationDemoFragment.java @@ -33,10 +33,6 @@ /** Demo of the MaterialDividerItemDecoration. */ public class DividerItemDecorationDemoFragment extends DemoFragment { - private RecyclerView recyclerView; - private DividerAdapter adapter; - private RecyclerView.LayoutManager layoutManager; - @Nullable @Override public View onCreateDemoView( @@ -47,24 +43,34 @@ public View onCreateDemoView( layoutInflater.inflate( R.layout.cat_divider_recyclerview_fragment, viewGroup, /* attachToRoot */ false); - recyclerView = view.findViewById(R.id.divider_recyclerview); - layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false); + RecyclerView recyclerViewHorizontal = view.findViewById(R.id.divider_recyclerview_horizontal); + RecyclerView recyclerViewVertical = view.findViewById(R.id.divider_recyclerview_vertical); + + setUpDividers(recyclerViewHorizontal, LinearLayoutManager.HORIZONTAL); + setUpDividers(recyclerViewVertical, LinearLayoutManager.VERTICAL); + + return view; + } + + private void setUpDividers(@NonNull RecyclerView recyclerView, int orientation) { + RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), + orientation, false); recyclerView.setLayoutManager(layoutManager); MaterialDividerItemDecoration divider = - new MaterialDividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL); + new MaterialDividerItemDecoration(getContext(), orientation); recyclerView.addItemDecoration(divider); - adapter = new DividerAdapter(); + DividerAdapter adapter = new DividerAdapter(); recyclerView.setAdapter(adapter); - - return view; } /** A RecyclerView adapter. */ private static final class DividerAdapter extends RecyclerView.Adapter { + private static final int ITEM_COUNT = 20; + /** Provide a reference to the views for each data item. */ private static class MyViewHolder extends RecyclerView.ViewHolder { @@ -95,7 +101,7 @@ public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { @Override public int getItemCount() { - return 30; + return ITEM_COUNT; } } } diff --git a/catalog/java/io/material/catalog/divider/res/layout/cat_divider_recyclerview_fragment.xml b/catalog/java/io/material/catalog/divider/res/layout/cat_divider_recyclerview_fragment.xml index 24ce5c31c13..be7817f23bf 100644 --- a/catalog/java/io/material/catalog/divider/res/layout/cat_divider_recyclerview_fragment.xml +++ b/catalog/java/io/material/catalog/divider/res/layout/cat_divider_recyclerview_fragment.xml @@ -15,9 +15,40 @@ limitations under the License. --> - + + android:layout_height="wrap_content" + android:layout_margin="16dp" + android:orientation="vertical"> + + + + + + + + + + diff --git a/catalog/java/io/material/catalog/divider/res/layout/cat_divider_recyclerview_item.xml b/catalog/java/io/material/catalog/divider/res/layout/cat_divider_recyclerview_item.xml index 2301ed67672..acaeccd4d10 100644 --- a/catalog/java/io/material/catalog/divider/res/layout/cat_divider_recyclerview_item.xml +++ b/catalog/java/io/material/catalog/divider/res/layout/cat_divider_recyclerview_item.xml @@ -17,8 +17,7 @@ diff --git a/catalog/java/io/material/catalog/divider/res/values/strings.xml b/catalog/java/io/material/catalog/divider/res/values/strings.xml index 49d7e1428d2..8a44849281a 100644 --- a/catalog/java/io/material/catalog/divider/res/values/strings.xml +++ b/catalog/java/io/material/catalog/divider/res/values/strings.xml @@ -66,4 +66,12 @@ description="Label of the item in the MaterialDividerItemDecoration demo [CHAR_LIMIT=20]"> Item %1$d + + Layout manager with horizontal orientation + + + Layout manager with vertical orientation + diff --git a/lib/java/com/google/android/material/divider/MaterialDividerItemDecoration.java b/lib/java/com/google/android/material/divider/MaterialDividerItemDecoration.java index 6d747567c66..d79a3c57f71 100644 --- a/lib/java/com/google/android/material/divider/MaterialDividerItemDecoration.java +++ b/lib/java/com/google/android/material/divider/MaterialDividerItemDecoration.java @@ -352,10 +352,11 @@ private void drawForHorizontalOrientation(@NonNull Canvas canvas, @NonNull Recyc bottom -= insetEnd; int childCount = parent.getChildCount(); - for (int i = 0; i < childCount; i++) { + int dividerCount = lastItemDecorated ? childCount : childCount - 1; + for (int i = 0; i < dividerCount; i++) { View child = parent.getChildAt(i); - parent.getLayoutManager().getDecoratedBoundsWithMargins(child, tempRect); - // Take into consideration any translationY added to the view. + parent.getDecoratedBoundsWithMargins(child, tempRect); + // Take into consideration any translationX added to the view. int right = tempRect.right + Math.round(child.getTranslationX()); int left = right - dividerDrawable.getIntrinsicWidth() - thickness; dividerDrawable.setBounds(left, top, right, bottom);