Skip to content

Commit

Permalink
[Divider] Fixed lastItemDecorated not being applied to horizontal ori…
Browse files Browse the repository at this point in the history
…entation. Also updated the demo to include both orientations.

Resolves #2802

PiperOrigin-RevId: 461247080
(cherry picked from commit 6eeb95f)
  • Loading branch information
leticiarossi authored and drchen committed Aug 4, 2022
1 parent a8ae9c4 commit 92caa19
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 22 deletions.
Expand Up @@ -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(
Expand All @@ -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<DividerAdapter.MyViewHolder> {

private static final int ITEM_COUNT = 20;

/** Provide a reference to the views for each data item. */
private static class MyViewHolder extends RecyclerView.ViewHolder {

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

@Override
public int getItemCount() {
return 30;
return ITEM_COUNT;
}
}
}
Expand Up @@ -15,9 +15,40 @@
limitations under the License.
-->

<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/divider_recyclerview"
android:layout_height="wrap_content"
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_margin="16dp"/>
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">

<!-- Horizontal orientation dividers. -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:textAppearance="?attr/textAppearanceTitleLarge"
android:text="@string/cat_divider_item_decoration_horizontal"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/divider_recyclerview_horizontal"
android:layout_marginBottom="16dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>

<!-- Vertical orientation dividers. -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:textAppearance="?attr/textAppearanceTitleLarge"
android:text="@string/cat_divider_item_decoration_vertical"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/divider_recyclerview_vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginBottom="48dp"/>
</LinearLayout>
</ScrollView>
Expand Up @@ -17,8 +17,7 @@

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_margin="8dp"
android:textAppearance="?attr/textAppearanceSubtitle1"/>
Expand Up @@ -66,4 +66,12 @@
description="Label of the item in the MaterialDividerItemDecoration demo [CHAR_LIMIT=20]">
Item %1$d
</string>
<string name="cat_divider_item_decoration_horizontal"
description="Subtitle label for dividers with horizontal orientation [CHAR_LIMIT=NONE]">
Layout manager with horizontal orientation
</string>
<string name="cat_divider_item_decoration_vertical"
description="Subtitle label for dividers with vertical orientation [CHAR_LIMIT=NONE]">
Layout manager with vertical orientation
</string>
</resources>
Expand Up @@ -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);
Expand Down

0 comments on commit 92caa19

Please sign in to comment.