我在每个子行中有两个按钮和一个文本视图。这两个按钮用作触发器来计算数字,然后在 textview 中写入数字。我的问题是,当我滚动 recyclerview 时,textview 的值总是会发生变化,有时会影响相邻子行上的 textview。任何人都可以帮忙吗?谢谢你。
当我单击第一行 (1) 上的 + 时,第三行 (2) 上的 textview 也发生了变化,当我向下滚动时,这些数字都丢失了。
我使用了这个库:https ://github.com/thoughtbot/expandable-recycler-view
这是我的代码:
MenuDeliveryRecyclerAdapter.java
public abstract class MenuDeliveryRecyclerAdapter extends ExpandableRecyclerViewAdapter<MenuDeliveryRecyclerAdapter.MenuViewHolder, MenuDeliveryRecyclerAdapter.SubmenuViewHolder> {
List<MenuItemListModel> list;
private LayoutInflater mInflator;
private Context context;
public MenuDeliveryRecyclerAdapter(Context context, @NonNull List<? extends ExpandableGroup> parentItemList) {
super(parentItemList);
this.context = context;
mInflator = LayoutInflater.from(context);
Timber.e("MenuItemListModel: %s", parentItemList.toString());
}
public void setData(List<MenuItemListModel> list){
this.list = list;
notifyDataSetChanged();
}
@Override
public MenuViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
View view = mInflator.inflate(R.layout.item_category, parent, false);
return new MenuViewHolder(view);
}
@Override
public SubmenuViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
View view = mInflator.inflate(R.layout.item_menu_reservasi, parent, false);
return new SubmenuViewHolder(view);
}
@Override
public void onBindChildViewHolder(SubmenuViewHolder holder, int flatPosition, ExpandableGroup group,
int childIndex) {
final MenuItemResponseModel menu = ((DeliveryExpandableModel) group).getItems().get(childIndex);
holder.bind(menu , childIndex);
}
@Override
public void onBindGroupViewHolder(MenuViewHolder holder, int flatPosition,
ExpandableGroup group) {
holder.category.setText(group.getTitle());
}
@Override
public int getItemCount() {
return super.getItemCount();
}
public class MenuViewHolder extends GroupViewHolder {
private TextView category;
public MenuViewHolder(View itemView) {
super(itemView);
category = (TextView) itemView.findViewById(R.id.category);
}
}
public class SubmenuViewHolder extends ChildViewHolder {
private ImageView gambarmenu;
private TextView namamenu;
private TextView hargamenu;
private Button count;
private Button btnMin;
private Button btnPlus;
public SubmenuViewHolder(View convertView) {
super(convertView);
gambarmenu = (ImageView) convertView.findViewById(R.id.gambarmenu);
namamenu = (TextView) convertView.findViewById(R.id.namamenu);
hargamenu = (TextView) convertView.findViewById(R.id.hargamenu);
count = (Button) convertView.findViewById(R.id.count);
btnMin = (Button) convertView.findViewById(R.id.btnMin);
btnPlus = (Button) convertView.findViewById(R.id.btnPlus);
}
public void bind(final MenuItemResponseModel childModel, final int childIndex) {
Glide.with(context).load(childModel.getGambar())
.thumbnail(0.5f)
.crossFade()
.into(gambarmenu);
namamenu.setText(childModel.getNama_menu());
hargamenu.setText(Utils.formatKursIndonesia(Double.valueOf(childModel.getHarga())));
btnMin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int jlh = Integer.valueOf(count.getText().toString());
count.setText(String.valueOf(jlh - 1 >= 0 ? jlh - 1 : 0));
notifyDataSetChanged();
//notifyItemChanged(childIndex);
onLeftMenuClick(childModel, jlh);
}
});
btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int jlh = Integer.valueOf(count.getText().toString());
count.setText(String.valueOf(jlh+1));
notifyDataSetChanged();
//notifyItemChanged(childIndex);
onRightMenuClick(childModel, jlh);
}
});
}
}
protected abstract void onRightMenuClick(MenuItemResponseModel parent, int jumlah);
protected abstract void onLeftMenuClick(MenuItemResponseModel parent, int jumlah);
}