javaandroidandroid-recyclerviewfastadapter

Not able to change the image color of the image view


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;
            }
        });

Solution

  • The FastAdapter's OnClickListener already provides you everything you need. It passes the clicked item, and also the Adapter 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 the state of that item to selected.

    There are multiple ways of automatically applying the color in that case:

    1. Use ColorStateList

    The easiest solution is to define a ColorStateList instead of a simple color for that item. For example you could have a Foreground and just define a translucent color if the state is selected. A very simple ColorStateList could look like this:

    return new ColorStateList(
                    new int[][]{
                            new int[]{android.R.attr.state_selected},
                            new int[]{}
                    },
                    new int[] {
                            colorSelected,
                            Color.TRANSPARENT
                    }
            );
    
    1. Automatically Notify the Adapter about the change

    The FastAdapter allows you to enable the bindView being called in case of the selection. Enable this via: FastAdapter.withSelectWithItemUpdate(true) After this the bindView method of that element is called and you can simply check for isSelected and define the ColorFilter or not

    1. Notify the Adapter manually

    If the adapter should not automatically call the notify method you can do this on your own also by doing fastAdapter.notifyAdapterItemChanged(position) (you can optional pass a payload in addition, so you can check for that one in the bindView method) after that check again in the bindView method for isSelected or not and handle the UI as you need it