点击时间

时间:2016-01-25 06:40:12

标签: android onclick android-recyclerview

我想显示一个水平列表,显示某些服务的非活动图像(如汽油泵,轮胎)。单击“回收”视图后,我必须更改非活动图像 使用活动图像。我将图像作为Url从服务中获取,然后下载并设置它。 所以我认为 here

所述的选择器不可能解决问题

任何帮助都应该受到赞赏。

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


    }
}

使用上面的代码我可以更改图像,但它会更改最后一张未单击的图像。 在点击之前我得到了这个 enter image description here

但在点击第二张图像后,它会将最后一张图像更改为enter image description here

1 个答案:

答案 0 :(得分:1)

在您的数据旁边存储一些布尔变量,例如isClicked将指示用户是否已单击图像,创建将更新该布尔值的图像侦听器,最后使用if / then语句在onBindViewHolder测试中,并根据当前数据的isClicked变量设置正确的图片。