Change item background according to the theme (#3480)

* Change item  background according to the theme

* Change background colour of item on being selected

* Change background colors using XML selectors
This commit is contained in:
Anmol Gupta 2020-03-16 15:43:20 +05:30 committed by GitHub
parent 81c7281d3a
commit 34a016d49d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 10 deletions

View file

@ -70,7 +70,7 @@ public class NearbyFilterSearchRecyclerViewAdapter
@NonNull @NonNull
@Override @Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = inflater.inflate(callback.isDarkTheme()?R.layout.nearby_search_list_item_dark:R.layout.nearby_search_list_item, parent, false); View itemView = inflater.inflate(callback.isDarkTheme() ? R.layout.nearby_search_list_item_dark : R.layout.nearby_search_list_item, parent, false);
return new RecyclerViewHolder(itemView); return new RecyclerViewHolder(itemView);
} }
@ -79,17 +79,20 @@ public class NearbyFilterSearchRecyclerViewAdapter
Label label = displayedLabels.get(position); Label label = displayedLabels.get(position);
holder.placeTypeIcon.setImageResource(label.getIcon()); holder.placeTypeIcon.setImageResource(label.getIcon());
holder.placeTypeLabel.setText(label.toString()); holder.placeTypeLabel.setText(label.toString());
holder.placeTypeLayout.setSelected(label.isSelected());
holder.placeTypeLayout.setBackgroundColor(label.isSelected() ? ContextCompat.getColor(context, R.color.divider_grey) : callback.isDarkTheme()?Color.BLACK:Color.WHITE);
holder.placeTypeLayout.setOnClickListener(view -> { holder.placeTypeLayout.setOnClickListener(view -> {
callback.setCheckboxUnknown(); callback.setCheckboxUnknown();
if (label.isSelected()) { if (label.isSelected()) {
selectedLabels.remove(label); selectedLabels.remove(label);
} else { } else {
selectedLabels.add(label); selectedLabels.add(label);
} }
label.setSelected(!label.isSelected()); label.setSelected(!label.isSelected());
holder.placeTypeLayout.setBackgroundColor(label.isSelected() ? ContextCompat.getColor(context, R.color.divider_grey) : Color.WHITE); holder.placeTypeLayout.setSelected(label.isSelected());
callback.filterByMarkerType(selectedLabels, 0, false, false); callback.filterByMarkerType(selectedLabels, 0, false, false);
}); });
} }
@ -165,7 +168,7 @@ public class NearbyFilterSearchRecyclerViewAdapter
notifyDataSetChanged(); notifyDataSetChanged();
} }
public interface Callback{ public interface Callback {
void setCheckboxUnknown(); void setCheckboxUnknown();

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@color/opak_middle_grey" />
<item android:drawable="@color/black" />
</selector>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@color/divider_grey" />
<item android:drawable="@color/white" />
</selector>

View file

@ -1,18 +1,20 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/search_list_item" android:id="@+id/search_list_item"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:orientation="horizontal"
android:padding="@dimen/tiny_margin"> android:padding="@dimen/tiny_margin"
android:background="@drawable/linearlayout_color_selector">
<ImageView <ImageView
android:id="@+id/place_icon" android:id="@+id/place_icon"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content"/>
<TextView <TextView
android:id="@+id/place_text" android:id="@+id/place_text"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"></TextView> android:layout_height="wrap_content"/>
</LinearLayout> </LinearLayout>

View file

@ -4,16 +4,17 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="@dimen/tiny_margin" android:padding="@dimen/tiny_margin"
android:orientation="horizontal"> android:orientation="horizontal"
android:background="@drawable/linearlayout_color_dark_selector">
<ImageView <ImageView
android:id="@+id/place_icon" android:id="@+id/place_icon"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content"/>
<TextView <TextView
android:id="@+id/place_text" android:id="@+id/place_text"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textColor="@color/white"></TextView> android:textColor="@color/white"/>
</LinearLayout> </LinearLayout>