I need some help here. I am fetching the data from the JSON.Fetching data. It consists of images and text and I am using the Fast Adapter(mike penz) to populate into recycler view, but when I select the specified row from the recycler view, it needs to change the image color. Where can I change the text view color by using the selector but I can't change the color of the image in the image view of the selected row. Please help me out. Here is the code:
service_type_adapter.withOnClickListener(new FastAdapter.OnClickListener<Service_Type_Adapter>() {
@Override
public boolean onClick(View v, IAdapter<Service_Type_Adapter> adapter, Service_Type_Adapter item, int position) {
AppCompatImageView service_image= (AppCompatImageView) v.findViewById(R.id.service_image);
int service_imagecolors = ContextCompat.getColor(getApplicationContext(), R.color.skyblue);
service_image.setColorFilter(service_imagecolors, PorterDuff.Mode.SRC_IN);
service_type_adapter.select(position);
if (lastselectedposition != -1) {
service_type_adapter.deselect(lastselectedposition);
}
lastselectedposition = position;
servicetypename = item.getServicename();
action = item.getServiceid();
googlemap.clear();
onMapReady(googlemap);
return true;
}
});
The FastAdapter's
OnClickListener
already provides you everything you need. It passes the clicked item, and also theAdapter
responsible for the specific item.So when the user clicks on the item (and you have enabled selection for that item) the
FastAdapter
will automatically set thestate
of that item to selected.There are multiple ways of automatically applying the color in that case:
The easiest solution is to define a
ColorStateList
instead of a simple color for that item. For example you could have aForeground
and just define a translucent color if the state is selected. A very simpleColorStateList
could look like this:Adapter
about the changeThe
FastAdapter
allows you to enable thebindView
being called in case of the selection. Enable this via:FastAdapter.withSelectWithItemUpdate(true)
After this thebindView
method of that element is called and you can simply check forisSelected
and define theColorFilter
or notAdapter
manuallyIf the adapter should not automatically call the
notify
method you can do this on your own also by doingfastAdapter.notifyAdapterItemChanged(position)
(you can optional pass a payload in addition, so you can check for that one in thebindView
method) after that check again in thebindView
method forisSelected
or not and handle the UI as you need it