Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android 13880 Hide last divider in XML Lists #327

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't also be the screenshot (the image) here in the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

}
}

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")
}
}

}

17 changes: 17 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,17 @@
<?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"
/>
Comment on lines +11 to +18
Copy link
Contributor

@jeprubio jeprubio Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this could also be created programmatically and inserted in the dummy layout. But I'm not against this, if we need to test several MisticaRecyclerView elements perhaps it's simpler this way. And it's only a layout in the test directory, should be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this approach is simpler and, as you said, the layout is only added to the test directory :)

</LinearLayout>