我有一个RecyclerView
,如下图所示。
我从服务器获取食品数据,并如上所述将其绑定到RecyclerView
。
在foodItemList
数组的foodItemTypeList
数组列表中分配食品。在foodItemTypeList
数组列表中,存储类型的值及其相应的价格。
我想要的是,当用户选择一种食物类型(例如中等)时,相应的单价会更新。
这是FoodItemAdapter
类:
public class FoodItemAdapter extends RecyclerView.Adapter<FoodItemAdapter.CustomViewHolder> {
private List<FoodItem> foodItemList;
Context context;
String token;
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView food_name, food_unit_price;
Spinner food_item_type;
public ImageView food_item_image;
public Button viewDetail;
public CustomViewHolder(View view) {
super(view);
food_name = (TextView) itemView.findViewById(R.id.food_name);
food_item_type = (Spinner) itemView.findViewById(R.id.food_item_type);
viewDetail = (Button) itemView.findViewById(R.id.viewDetail);
food_unit_price = (TextView) itemView.findViewById(R.id.food_unit_price);
food_item_image = (ImageView) itemView.findViewById(R.id.food_item_image);
}
}
public FoodItemAdapter(Context context, List<FoodItem> foodItemList,String token) {
this.foodItemList = foodItemList;
this.context = context;
this.token = token;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.food_items_item, parent, false);
return new CustomViewHolder(itemView);
}
@Override
public void onBindViewHolder(final CustomViewHolder holder, int position) {
FoodItem foodItem = foodItemList.get(position);
holder.food_name.setText(foodItem.getFood_name());
holder.viewDetail.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
}
});
holder.food_unit_price.setText(foodItem.getFood_unit_price()+" AF");
if(foodItem.getFood_item_image()!=null && !foodItem.getFood_item_image().isEmpty()){
Picasso.get()
.load(foodItem.getFood_item_image())
// To fit image into imageView
.resize(50, 50)
.centerCrop()
.into(holder.food_item_image);
} else {
Log.d("Food Item Image:", "Food Item image is either empty or null");
}
List<FoodItemType> foodItemTypeList = new ArrayList<>();
foodItemTypeList = foodItem.getFoodItemTypeList();
ArrayAdapter userAdapter = new ArrayAdapter(context, R.layout.spinner, foodItemTypeList);
holder.food_item_type.setAdapter(userAdapter);
holder.food_item_type.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
FoodItemType foodItemType = (FoodItemType) (holder.food_item_type).getSelectedItem();
Toast.makeText(context, "Clicked: " +
foodItemType.getFood_unit_price(), Toast.LENGTH_LONG).show();
FoodItem foodItemNew = new FoodItem(foodItemList.get(holder.getAdapterPosition()).getFood_item_id(),foodItemList.get(holder.getAdapterPosition()).getFood_category_id(),foodItemList.get(holder.getAdapterPosition()).getFood_name(),foodItemList.get(holder.getAdapterPosition()).getFood_item_image(),foodItemList.get(holder.getAdapterPosition()).getFood_item_desc(), foodItemType.getFood_item_type_id(),foodItemType.getFood_item_type_name(),foodItemType.getFood_unit_price(), foodItemList.get(holder.getAdapterPosition()).getFoodItemTypeList());
foodItemList.set(holder.getAdapterPosition(),foodItemNew);
notifyItemChanged(holder.getAdapterPosition());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// todo for nothing selected
}
});
}
@Override
public int getItemCount() {
return foodItemList.size();
}
}
我的FoodItemsFragment
类的一部分如下:
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_food_item);
foodItemAdapter = new FoodItemAdapter(getActivity(),foodItemList, token);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(foodItemAdapter);
foodItemAdapter.notifyDataSetChanged();
我的问题是:滚动RecyclerView
时,视图会多次加载(有点滞后),而更改食物类型(例如:更改为中)时,什么也没发生。
感谢您提供任何帮助。
答案 0 :(得分:0)
我建议您在FoodItem
类中的另一个字段为selectedFoodType
。这将保存所选类型的索引。默认情况下,将值初始化为0,并且从下拉列表中选择一项时,只需相应地更新相应的FoodItem
的{{1}}。
另一件事是,您不必每次更改selectedFoodType
的类型时都创建一个新的FoodItem
。您只需要拥有FoodItem
,然后调用selectedFoodType
。
因此,代码应如下所示。在notifyDataSetChanged()
函数内部,执行以下操作。
onItemSelected
请注意,我尚未测试此代码。请根据您的需要进行修改。希望有帮助!