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
OnClickListeneralready provides you everything you need. It passes the clicked item, and also theAdapterresponsible for the specific item.So when the user clicks on the item (and you have enabled selection for that item) the
FastAdapterwill automatically set thestateof that item to selected.There are multiple ways of automatically applying the color in that case:
The easiest solution is to define a
ColorStateListinstead of a simple color for that item. For example you could have aForegroundand just define a translucent color if the state is selected. A very simpleColorStateListcould look like this:Adapterabout the changeThe
FastAdapterallows you to enable thebindViewbeing called in case of the selection. Enable this via:FastAdapter.withSelectWithItemUpdate(true)After this thebindViewmethod of that element is called and you can simply check forisSelectedand define theColorFilteror notAdaptermanuallyIf the adapter should not automatically call the
notifymethod 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 thebindViewmethod) after that check again in thebindViewmethod forisSelectedor not and handle the UI as you need it