Skip to content

Commit

Permalink
Android 13880 Hide last divider in XML Lists (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmartinbTEF authored Jan 11, 2024
1 parent 2d26365 commit afb1e1a
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 6 deletions.
Binary file modified library/screenshots/check_ListRowItem_with_32x32_asset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified library/screenshots/check_ListRowItem_with_64x64_asset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified library/screenshots/check_ListRowView_xml.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -35,23 +35,25 @@ 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)
}
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DummyActivity>()

@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<ListViewHolder>() {
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")
}
}

}

19 changes: 19 additions & 0 deletions library/src/test/res/layout/test_mistica_recycler_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/test_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="vertical"
>

<com.telefonica.mistica.list.MisticaRecyclerView
android:id="@+id/test_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:nestedScrollingEnabled="false"
app:listLayoutType="full_width"
/>
</LinearLayout>

0 comments on commit afb1e1a

Please sign in to comment.