1

I want to show a horizontal list that would show inactive image of some services (like petrol pump,tyre).After click Recycle view i have to change inactive images with active image.I take images as Url from service and then download and set it. So i think its not possible solution by selector as stated by here

Any help should be appreciated.

public class ServicesCategoryadapter extends RecyclerView.Adapter<ServicesCategoryadapter.ViewHolder> {



    public ServicesCategoryadapter(Context context, ArrayList<ServiceCategory> serviceCategoryArrayList, GoogleMap googleMap, GetVendorsService getVendorsService, LatLng latLng, LinearLayout subcategoryLayout, RecyclerView subCategoryRecycle) {
        this.context = context;
        this.serviceCategoryArrayList = serviceCategoryArrayList;
        this.googleMap = googleMap;
        mPrefscmodel = context.getSharedPreferences(MY_PREFS_NAME, 0);
        editor = mPrefscmodel.edit();
        this.getVendorsService = getVendorsService;
        this.latLng = latLng;
        this.subcategoryLayout = subcategoryLayout;
        this.subCategoryRecycle = subCategoryRecycle;
    }


    SharedPreferences mPrefscmodel;
    private static SharedPreferences.Editor editor;

    @Override
    public ServicesCategoryadapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.twowayview, null);

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {


        setImageView(viewHolder.vehicleImage, serviceCategoryArrayList.get(i).getInactiveimage(), i);
        viewHolder.serviceCategoryName.setText(serviceCategoryArrayList.get(i).getCategoryName());
        viewHolder.serviceCategoryName.setTextSize(8.0f);

    }

    @Override
    public int getItemCount() {
        return serviceCategoryArrayList.size();
    }

    // inner class to hold a reference to each item of RecyclerView
    public static class ViewHolder extends RecyclerView.ViewHolder {


        public  static  ImageView vehicleImage;
        public TextView serviceCategoryName;
        static   String image="";

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);

            vehicleImage = (ImageView) itemLayoutView.findViewById(R.id.twowayviewimage);
            serviceCategoryName = (TextView) itemLayoutView.findViewById(R.id.serviceCategoryName);
            itemLayoutView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HomeFragment.isServiceCategory = false;
                    int position = getLayoutPosition();
                    ViewHolder viewHolder = (ViewHolder) v.getTag();


                    String serviceCategoryId = serviceCategoryArrayList.get(position).getCategoriesId();
                    String serviceCategoryName = serviceCategoryArrayList.get(position).getCategoryName();
                    getVendorsService = new GetVendorsService(context, serviceCategoryId, googleMap, serviceCategoryName, latLng);
                    getVendorsService.execute();
                    editor.putString("serviceCategoryId", serviceCategoryId);
                    editor.commit();
                   image=serviceCategoryArrayList.get(position).getImage();

                    ArrayList<ServiceCategory.subServiceCategory> subServiceCategoryObj = new ArrayList<ServiceCategory.subServiceCategory>();
                    subServiceCategoryObj = serviceCategoryArrayList.get(position).getSubServiceCategories();
                    if (subServiceCategoryObj.size() != 0) {
                        subcategoryLayout.setVisibility(View.VISIBLE);
                        SubCategoryAdapter subCategoryAdapter = new SubCategoryAdapter(context, subServiceCategoryObj);
                        subCategoryRecycle.setAdapter(subCategoryAdapter);
                    }
                    else
                        subcategoryLayout.setVisibility(View.GONE);

                }
            });

        }

    }

    public static  void setImageView( ImageView imageView, String url, int i) {

        new ImageDownLoadService(context, imageView).execute(url);


    }
}

With above code I am able to change image but it change last image not clicked. Before click I get this out put enter image description here

but after clicking second image that is pump service it changes last image as enter image description here

Community
  • 1
  • 1
Rahul Patel
  • 121
  • 1
  • 8

1 Answers1

1

Store some boolean variable next to your data e.g. isClicked that will indicate if the user has clicked on image, create a image listener that will update that boolean, and lastly in your onBindViewHolder test with if/then statement and set the correct picture based on your isClicked variable for the current data.