gpt4 book ai didi

android - 在点击时间设置 recyclerview 项目

转载 作者:行者123 更新时间:2023-11-30 01:37:04 29 4
gpt4 key购买 nike

我想显示一个水平列表,显示某些服务(如汽油泵、轮胎)的非 Activity 图像。单击回收 View 后,我必须更改非 Activity 图像带有 Activity 图像。我从服务中获取图像作为 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

最佳答案

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

关于android - 在点击时间设置 recyclerview 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34986503/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com