diff --git a/library/screenshots/check_ListRowItem_with_32x32_asset.png b/library/screenshots/check_ListRowItem_with_32x32_asset.png index cf495f640..d7c618bc2 100644 Binary files a/library/screenshots/check_ListRowItem_with_32x32_asset.png and b/library/screenshots/check_ListRowItem_with_32x32_asset.png differ diff --git a/library/screenshots/check_ListRowItem_with_64x64_asset.png b/library/screenshots/check_ListRowItem_with_64x64_asset.png index acab68941..38054778a 100644 Binary files a/library/screenshots/check_ListRowItem_with_64x64_asset.png and b/library/screenshots/check_ListRowItem_with_64x64_asset.png differ diff --git a/library/screenshots/check_ListRowView_xml.png b/library/screenshots/check_ListRowView_xml.png index 3fc408451..6e6365111 100644 Binary files a/library/screenshots/check_ListRowView_xml.png and b/library/screenshots/check_ListRowView_xml.png differ diff --git a/library/screenshots/check_MisticaRecyclerView_xml_hides_last_divider.png b/library/screenshots/check_MisticaRecyclerView_xml_hides_last_divider.png new file mode 100644 index 000000000..693d11a3d Binary files /dev/null and b/library/screenshots/check_MisticaRecyclerView_xml_hides_last_divider.png differ diff --git a/library/src/main/java/com/telefonica/mistica/list/decoration/divider/DividerItemDecoration.kt b/library/src/main/java/com/telefonica/mistica/list/decoration/divider/DividerItemDecoration.kt index 64185a1e1..18eeb33a6 100644 --- a/library/src/main/java/com/telefonica/mistica/list/decoration/divider/DividerItemDecoration.kt +++ b/library/src/main/java/com/telefonica/mistica/list/decoration/divider/DividerItemDecoration.kt @@ -24,7 +24,7 @@ open class DividerItemDecoration @JvmOverloads constructor( override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) { super.onDrawOver(c, parent, state) divider?.let { divider -> - drawHorizontal(c, parent, divider) + drawHorizontal(c, parent, divider, state.itemCount) } } @@ -35,7 +35,7 @@ open class DividerItemDecoration @JvmOverloads constructor( state: RecyclerView.State, ) { val position = parent.getChildAdapterPosition(view) - if (hasDivider(position)) { + if (hasDivider(position, state.getItemCount())) { outRect.set(0, 0, 0, divider?.intrinsicHeight ?: 0) } else { outRect.set(0, 0, 0, 0) @@ -43,15 +43,17 @@ open class DividerItemDecoration @JvmOverloads constructor( drawableState = view.drawableState } - private fun hasDivider(position: Int): Boolean = - position != RecyclerView.NO_POSITION && adapter.hasDivider(position) + private fun hasDivider(position: Int, items: Int): Boolean = + position != RecyclerView.NO_POSITION && adapter.hasDivider(position) && !isLastElement(position, items) - private fun drawHorizontal(c: Canvas, parent: RecyclerView, divider: Drawable) { + private fun isLastElement(position: Int, items: Int): Boolean = position == items - 1 + + private fun drawHorizontal(c: Canvas, parent: RecyclerView, divider: Drawable, items: Int) { c.save() for (i in 0 until parent.childCount) { val child = parent.getChildAt(i) val position = parent.getChildAdapterPosition(child) - if (!hasDivider(position)) { + if (!hasDivider(position, items)) { continue } parent.getDecoratedBoundsWithMargins(child, bounds) diff --git a/library/src/test/java/com/telefonica/mistica/list/MisticaRecyclerViewTest.kt b/library/src/test/java/com/telefonica/mistica/list/MisticaRecyclerViewTest.kt new file mode 100644 index 000000000..3d3a10e1a --- /dev/null +++ b/library/src/test/java/com/telefonica/mistica/list/MisticaRecyclerViewTest.kt @@ -0,0 +1,56 @@ +package com.telefonica.mistica.list + +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.FrameLayout +import androidx.recyclerview.widget.RecyclerView +import androidx.test.espresso.Espresso +import androidx.test.espresso.matcher.ViewMatchers +import androidx.test.ext.junit.rules.activityScenarioRule +import com.telefonica.mistica.DummyActivity +import com.telefonica.mistica.R +import com.telefonica.mistica.testutils.ScreenshotsTest +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +internal class MisticaRecyclerViewTest: ScreenshotsTest() { + @get:Rule + val rule = activityScenarioRule() + + @Test + fun `check MisticaRecyclerView xml hides last divider`() { + rule.scenario.onActivity { activity -> + val wrapper: FrameLayout = activity.findViewById(R.id.dummy_activity_wrapper) + val view = activity.layoutInflater.inflate(R.layout.test_mistica_recycler_view, wrapper, false) + activity.setContentView(view) + + val list: MisticaRecyclerView = view.findViewById(R.id.test_list) + list.adapter = ClickableListAdapter() + compareScreenshot(Espresso.onView(ViewMatchers.withId(R.id.test_recycler_view))) + } + } + + class ListViewHolder(val rowView: ListRowView) : RecyclerView.ViewHolder(rowView) + class ClickableListAdapter : RecyclerView.Adapter() { + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder { + return ListViewHolder( + LayoutInflater.from(parent.context).inflate( + R.layout.sheet_list_row_selectable_item, + parent, + false + ) as ListRowView + ) + } + + override fun getItemCount(): Int = 2 + + override fun onBindViewHolder(holder: ListViewHolder, position: Int) { + holder.rowView.setTitle("Row $position") + } + } + +} + diff --git a/library/src/test/res/layout/test_mistica_recycler_view.xml b/library/src/test/res/layout/test_mistica_recycler_view.xml new file mode 100644 index 000000000..5c7690e23 --- /dev/null +++ b/library/src/test/res/layout/test_mistica_recycler_view.xml @@ -0,0 +1,19 @@ + + + + + \ No newline at end of file