11package com.backbase.deferredresources.demo
22
33import android.graphics.Color
4+ import android.graphics.drawable.GradientDrawable
45import android.view.ViewGroup
56import android.widget.LinearLayout
67import androidx.recyclerview.widget.RecyclerView
78import com.backbase.deferredresources.DeferredColor
9+ import com.backbase.deferredresources.DeferredDrawable
810import com.backbase.deferredresources.DeferredFormattedPlurals
911import com.backbase.deferredresources.DeferredText
1012
1113class DemoPagerAdapter : RecyclerView .Adapter <DemoPagerAdapter .DeferredResourceViewHolder >() {
1214
1315 private val formattedPluralsResource = DeferredFormattedPlurals .Resource (R .plurals.horses)
16+ private val oval by lazy {
17+ GradientDrawable (
18+ GradientDrawable .Orientation .BL_TR ,
19+ intArrayOf(Color .parseColor(" #00ff00" ), Color .parseColor(" #000fff" ))
20+ ).apply {
21+ gradientType = GradientDrawable .RADIAL_GRADIENT
22+ cornerRadius = 0.8f
23+ shape = GradientDrawable .OVAL
24+ }
25+ }
1426
1527 fun getPageName (position : Int ): CharSequence = when (position) {
1628 0 -> " Colors"
1729 1 -> " Plurals resource"
30+ 2 -> " Drawables"
1831 else -> throw IllegalArgumentException (" Position $position in adapter with size $itemCount " )
1932 }
2033
21- override fun getItemCount (): Int = 2
34+ override fun getItemCount (): Int = ViewType .values().size
2235
2336 override fun getItemViewType (position : Int ): Int = when (position) {
2437 0 -> ViewType .COLORS .ordinal
2538 1 -> ViewType .PLURALS .ordinal
39+ 2 -> ViewType .DRAWABLES .ordinal
2640 else -> throw IndexOutOfBoundsException (" Position $position in adapter with size $itemCount " )
2741 }
2842
2943 override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): DeferredResourceViewHolder {
3044 val view = when (ViewType .values()[viewType]) {
3145 ViewType .COLORS -> DeferredColorsView (parent.context)
3246 ViewType .PLURALS -> DeferredPluralsView (parent.context)
47+ ViewType .DRAWABLES -> DeferredDrawablesView (parent.context)
3348 }.apply {
3449 layoutParams = LinearLayout .LayoutParams (
3550 ViewGroup .LayoutParams .MATCH_PARENT ,
@@ -58,6 +73,14 @@ class DemoPagerAdapter : RecyclerView.Adapter<DemoPagerAdapter.DeferredResourceV
5873 is DeferredPluralsView -> when (position) {
5974 1 -> view.display(formattedPluralsResource)
6075 }
76+ is DeferredDrawablesView -> {
77+ view.display(DeferredDrawable .Constant (oval), DeferredText .Constant (" Constant: " ))
78+ view.display(DeferredDrawable .Resource (R .drawable.oval), DeferredText .Constant (" Resource: " ))
79+ view.display(
80+ DeferredDrawable .Attribute (android.R .attr.homeAsUpIndicator),
81+ DeferredText .Constant (" Attribute: " )
82+ )
83+ }
6184 }
6285 }
6386
@@ -66,6 +89,6 @@ class DemoPagerAdapter : RecyclerView.Adapter<DemoPagerAdapter.DeferredResourceV
6689 ) : RecyclerView.ViewHolder(root)
6790
6891 private enum class ViewType {
69- COLORS , PLURALS
92+ COLORS , PLURALS , DRAWABLES
7093 }
7194}
0 commit comments