1
1
package com .ehsanmashhadi .library .view ;
2
2
3
+ import android .content .res .Resources ;
4
+ import android .util .TypedValue ;
3
5
import android .view .LayoutInflater ;
4
6
import android .view .View ;
5
7
import android .view .ViewGroup ;
11
13
12
14
import java .util .List ;
13
15
16
+ import androidx .annotation .ColorInt ;
14
17
import androidx .annotation .NonNull ;
15
18
import androidx .recyclerview .widget .RecyclerView ;
16
19
@@ -45,6 +48,7 @@ void setListener(OnCountryClickListener onCountryClickListener) {
45
48
public CountryHolder onCreateViewHolder (@ NonNull ViewGroup parent , int viewType ) {
46
49
47
50
View itemView = LayoutInflater .from (parent .getContext ()).inflate (R .layout .row_country , parent , false );
51
+
48
52
return new CountryHolder (itemView , position -> {
49
53
mOnCountryClickListener .onCountrySelected (mCountryList .get (position ));
50
54
itemView .setSelected (true );
@@ -66,8 +70,20 @@ public void onBindViewHolder(@NonNull CountryHolder holder, int position) {
66
70
if (sPreselectCountry != null )
67
71
if (mCountryList .get (position ).getName ().toLowerCase ().equals (sPreselectCountry .toLowerCase ())) {
68
72
holder .itemView .setSelected (true );
73
+ //Not setting background in XML because of not supporting reference attributes in pre-lollipop Android version
74
+ TypedValue typedValue = new TypedValue ();
75
+ Resources .Theme theme = holder .itemView .getContext ().getTheme ();
76
+ theme .resolveAttribute (R .attr .rowBackgroundSelectedColor , typedValue , true );
77
+ @ ColorInt int color = typedValue .data ;
78
+ holder .itemView .setBackgroundColor (color );
79
+
69
80
} else {
70
81
holder .itemView .setSelected (false );
82
+ TypedValue typedValue = new TypedValue ();
83
+ Resources .Theme theme = holder .itemView .getContext ().getTheme ();
84
+ theme .resolveAttribute (R .attr .rowBackgroundColor , typedValue , true );
85
+ @ ColorInt int color = typedValue .data ;
86
+ holder .itemView .setBackgroundColor (color );
71
87
}
72
88
if (sShowingFlag )
73
89
holder .mImageViewFlag .setVisibility (View .VISIBLE );
0 commit comments