Skip to content

Commit

Permalink
Merge pull request #180 from MostafaMohamed2002/feature/rv_divider
Browse files Browse the repository at this point in the history
Refactor RecyclerView item decoration in AboutFragment and SettingsFr…
  • Loading branch information
andrewtavis authored Oct 13, 2024
2 parents 4511f77 + 597876d commit efc20ef
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 39 deletions.
25 changes: 23 additions & 2 deletions app/src/main/java/be/scri/fragments/AboutFragment.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package be.scri.fragments

import CustomDividerItemDecoration
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
Expand All @@ -12,8 +13,10 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.addCallback
import androidx.appcompat.content.res.AppCompatResources.getDrawable
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import be.scri.BuildConfig
import be.scri.R
import be.scri.activities.MainActivity
Expand Down Expand Up @@ -56,16 +59,34 @@ class AboutFragment : Fragment() {
recyclerView1.layoutManager = LinearLayoutManager(context)
recyclerView1.adapter = CustomAdapter(getFirstRecyclerViewData(), requireContext())
recyclerView1.suppressLayout(true)

recyclerView1.apply {
addCustomItemDecoration()
}
val recyclerView2 = binding.recycleView
recyclerView2.layoutManager = LinearLayoutManager(context)
recyclerView2.adapter = CustomAdapter(getSecondRecyclerViewData(), requireContext())
recyclerView2.suppressLayout(true)

recyclerView2.apply {
addCustomItemDecoration()
}
val recyclerView3 = binding.recycleView3
recyclerView3.layoutManager = LinearLayoutManager(context)
recyclerView3.adapter = CustomAdapter(getThirdRecyclerViewData(), requireContext())
recyclerView3.suppressLayout(true)
recyclerView3.apply {
addCustomItemDecoration()
}
}

private fun RecyclerView.addCustomItemDecoration() {
val itemDecoration =
CustomDividerItemDecoration(
drawable = getDrawable(requireContext(), R.drawable.rv_divider)!!,
width = 1,
marginLeft = 50,
marginRight = 50,
)
addItemDecoration(itemDecoration)
}

private fun getFirstRecyclerViewData(): List<Any> =
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/be/scri/fragments/SettingsFragment.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package be.scri.fragments

import CustomDividerItemDecoration
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
Expand All @@ -16,6 +17,7 @@ import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.activity.addCallback
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.content.res.AppCompatResources.getDrawable
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import be.scri.R
Expand Down Expand Up @@ -145,6 +147,16 @@ class SettingsFragment : Fragment() {
recyclerView.layoutManager = LinearLayoutManager(requireContext())
recyclerView.adapter = adapter
recyclerView.suppressLayout(true)
recyclerView.apply {
val itemDecoration =
CustomDividerItemDecoration(
drawable = getDrawable(requireContext(), R.drawable.rv_divider)!!,
width = 1,
marginLeft = 50,
marginRight = 50,
)
addItemDecoration(itemDecoration)
}
}

private fun getRecyclerViewElements(): MutableList<TextItem> {
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/be/scri/views/CustomDividerItemDecoration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView

class CustomDividerItemDecoration(
private val drawable: Drawable,
private val width: Int,
private val marginLeft: Int,
private val marginRight: Int,
) : RecyclerView.ItemDecoration() {
override fun onDraw(
@NonNull canvas: Canvas,
@NonNull parent: RecyclerView,
@NonNull state: RecyclerView.State,
) {
val left = parent.paddingLeft + marginLeft
val right = parent.width - parent.paddingRight - marginRight

val childCount = parent.childCount
for (i in 0 until childCount - 1) { // Exclude the last item
val child = parent.getChildAt(i)
val params = child.layoutParams as RecyclerView.LayoutParams
val top = child.bottom + params.bottomMargin
val bottom = top + width

drawable.setBounds(left, top, right, bottom)
drawable.draw(canvas)
}
}

override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State,
) {
outRect.set(0, 0, 0, width)
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/drawable/rv_divider.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="1dp" />
<corners android:radius="10dp" />
<solid android:color="@color/rv_divider" />
</shape>
15 changes: 4 additions & 11 deletions app/src/main/res/layout/card_view_text.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cvItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:cardCornerRadius="5dp"
app:cardElevation="1dp">


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
Expand Down Expand Up @@ -54,4 +48,3 @@
android:textColor="@android:color/darker_gray"
android:textStyle="normal" />
</LinearLayout>
</androidx.cardview.widget.CardView>
14 changes: 3 additions & 11 deletions app/src/main/res/layout/card_view_with_image.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cvItem"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:cardCornerRadius="5dp"
app:cardElevation="1dp">


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
Expand Down Expand Up @@ -43,4 +36,3 @@
tools:srcCompat="@tools:sample/avatars" />

</LinearLayout>
</androidx.cardview.widget.CardView>
14 changes: 3 additions & 11 deletions app/src/main/res/layout/card_view_with_switch.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cvItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:cardCornerRadius="5dp"
app:cardElevation="1dp">

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
Expand Down Expand Up @@ -58,4 +51,3 @@
android:textStyle="normal" />

</LinearLayout>
</androidx.cardview.widget.CardView>
11 changes: 9 additions & 2 deletions app/src/main/res/layout/fragment_about.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
android:id="@+id/recycleView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/rounded_all_corners"
android:layout_marginTop="10dp" />

<TextView
Expand All @@ -44,6 +47,9 @@
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/rounded_all_corners"
android:layout_marginTop="10dp" />

<TextView
Expand All @@ -61,8 +67,9 @@
android:id="@+id/recycleView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="50dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/rounded_all_corners"
android:layout_marginTop="10dp"/>
</LinearLayout>
</ScrollView>

4 changes: 4 additions & 0 deletions app/src/main/res/layout/fragment_language_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:padding="12dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
Expand Down Expand Up @@ -61,6 +63,8 @@
android:layout_marginTop="20dp"
android:padding="12dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/functionalityRecyclerView" />
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
android:id="@+id/recyclerViewSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/rounded_all_corners"

/>

<TextView
Expand Down Expand Up @@ -74,10 +78,12 @@
android:id="@+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/rounded_all_corners"

/>
</RelativeLayout>

</LinearLayout>
</ScrollView>


1 change: 1 addition & 0 deletions app/src/main/res/values-night-v31/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<color name="corner_polygon_color">@color/dark_tutorial_button_color</color>
<color name="button_outline_color">@color/dark_button_outline_color</color>
<color name="button_text_color">#D17B0F</color>
<color name="rv_divider">#b2aeae</color>
<color name="switch_thumb_selector_color_true">#D17B0F</color>
<color name="switch_thumb_selector_color_false">@android:color/darker_gray</color>
<color name="switch_selector_color">@color/corner_polygon_color</color>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-v31/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<color name="corner_polygon_color">@color/light_tutorial_button_color</color>
<color name="button_outline_color">@color/light_tutorial_button_color</color>
<color name="button_text_color">#000000</color>
<color name="rv_divider">#d7d7d9</color>
<color name="switch_thumb_selector_color_true">#FDAD0D</color>
<color name="switch_thumb_selector_color_false">@android:color/darker_gray</color>
<color name="switch_selector_color">#FEDE9E</color>
Expand Down

0 comments on commit efc20ef

Please sign in to comment.