1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.customization.picker.common.ui.view 17 18 import android.graphics.Rect 19 import android.view.View 20 import androidx.recyclerview.widget.RecyclerView 21 22 /** Item spacing used by the horizontal RecyclerView with only 1 row. */ 23 class SingleRowListItemSpacing( 24 private val edgeItemSpacePx: Int, 25 private val itemHorizontalSpacePx: Int, 26 ) : RecyclerView.ItemDecoration() { getItemOffsetsnull27 override fun getItemOffsets( 28 outRect: Rect, 29 view: View, 30 parent: RecyclerView, 31 state: RecyclerView.State, 32 ) { 33 val itemIndex = parent.getChildAdapterPosition(view) 34 val itemCount = parent.adapter?.itemCount ?: 0 35 val isRtl = parent.layoutManager?.layoutDirection == View.LAYOUT_DIRECTION_RTL 36 when (itemIndex) { 37 0 -> { 38 outRect.left = if (!isRtl) edgeItemSpacePx else itemHorizontalSpacePx 39 outRect.right = if (isRtl) edgeItemSpacePx else itemHorizontalSpacePx 40 } 41 itemCount - 1 -> { 42 outRect.right = if (!isRtl) edgeItemSpacePx else itemHorizontalSpacePx 43 outRect.left = if (isRtl) edgeItemSpacePx else itemHorizontalSpacePx 44 } 45 else -> { 46 outRect.left = itemHorizontalSpacePx 47 outRect.right = itemHorizontalSpacePx 48 } 49 } 50 } 51 } 52