可扩展的RecyclerView:滚动时更改了子内部edittext的值

时间:2016-11-30 03:17:40

标签: android listview android-recyclerview expandablerecyclerview

每个子行中有两个按钮和一个textview。这两个按钮用作触发器来计算数字,然后在textview中写入数字。我的问题是,当我滚动recyclerview时,textview的值总是会改变,有时会影响邻居子行的textview。谁有人可以帮忙?谢谢。

when i click + on first row (1), textview on third row(2) is also changed.

当我点击第一行(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);
}

0 个答案:

没有答案