滚动回收器视图时如何避免recyclelarview中的重复数据

时间:2016-06-05 06:49:39

标签: android hashmap android-recyclerview recycler-adapter

我们第一次使用recyclarview。我的问题是当滚动循环重复数据添加到视图时。在下面的代码中:

public class ProductOptions_RecyclerAdapter extends RecyclerView.Adapter<ProductOptions_ListRowHolder> {


    private ArrayList<Product_Details> storeItemList;
    HashMap<String, List<Product_Details>> product_items = new HashMap<String, List<Product_Details>>();
    private Context mContext;
    List<String> arrlist_skuitemName = new ArrayList<String>();
    List<String> arrlist_skuitemLabel = new ArrayList<String>();


    public ProductOptions_RecyclerAdapter(Context context, List<String> arrlist_skuitemName, List<String> arrlist_skuitemLabel, HashMap<String, List<Product_Details>> product_items) {
        this.product_items = product_items;
        this.mContext = context;
        this.arrlist_skuitemName = arrlist_skuitemName;
        this.arrlist_skuitemLabel = arrlist_skuitemLabel;
    }


    @Override
    public ProductOptions_ListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_productoptions, null);

        ProductOptions_ListRowHolder store = new ProductOptions_ListRowHolder(v);

        return store;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(ProductOptions_ListRowHolder holder, int position) {

        holder.name.setText(Html.fromHtml(arrlist_skuitemName.get(position)));
        holder.label.setText(Html.fromHtml(arrlist_skuitemLabel.get(position)));

        List<Product_Details> items = product_items.get(arrlist_skuitemName.get(position));

        Log.d("listdata","min postion"+position+" / "+items.size());

        for (int i = 0; i < items.size(); i++) {
            RadioButton[] rb = new RadioButton[items.size()];

            Product_Details pd = items.get(i);
            rb[i] = new RadioButton(mContext);
            rb[i].setTextColor(ColorStateList.valueOf(Color.parseColor("#000000")));
            rb[i].setButtonTintList(ColorStateList.valueOf(Color.parseColor("#ff9900")));
            holder.rd_option.addView(rb[i]);
            rb[i].setText(pd.getName());

            TextView tv = new TextView(mContext);
            LinearLayout.LayoutParams buttonParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            buttonParams1.gravity = Gravity.END;
            tv.setLayoutParams(buttonParams1);
            tv.setText(pd.getSalePrice());
            tv.setTextSize(15);
            //  tv.setGravity(Gravity.RIGHT| Gravity.END);
            //holder.ll_priceitem.addView(tv);
            holder.rd_option.addView(tv);

        }

        holder.rd_option.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                int radioButtonID = group.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) group.findViewById(radioButtonID);

                Toast.makeText(mContext,radioButton.getText(),Toast.LENGTH_LONG).show();

            }
        });



        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ProductOptions_ListRowHolder tt = (ProductOptions_ListRowHolder) v.getTag();

                int pos = tt.getPosition();

            }
        };

        //Handle click event on both title and image click
        holder.rd_option.setOnClickListener(clickListener);

        holder.rd_option.setTag(holder);

//        notifyDataSetChanged();


    }

    @Override
    public int getItemCount() {
        return (null != product_items ? product_items.size() : 0);
    }

    @Override
    public int getItemViewType(int position) {

        return super.getItemViewType(position);
    }
}

在此Product_Details中是一个模型类。我们正在尝试根据服务响应动态添加radiobuttons,即添加到HashMap(product_items)。 当我们滚动循环视图时,自动重复项正在添加。 对于活动创建的数据是正确的。滚动重复时添加。 请指导我们,我们哪里出错了? 在此先感谢。

0 个答案:

没有答案