从recyclerview删除所有卡片视图,然后添加新的卡片视图-添加相同的卡片视图

时间:2018-10-01 07:31:11

标签: android android-recyclerview

当用户按下FAB时,卡片视图将添加到回收站视图。每个Cardview内部都有一个复选框。我要这样做,以便当用户在特定卡片视图中打勾时,该特定卡片视图被删除。

(仅供参考,每个Cardview都有一个微调框和一个复选框,以及一个textview和edittext)

我按了4次fab键,以将4张卡片视图添加到屏幕上。然后,我从微调器中挑选了不同的东西,看看发生了什么。当我勾选复选框时,该cardview将删除。我在所有卡片视图的复选框中打了勾,直到没有卡片视图为止。

但是当我按下晶圆厂以添加新的cardview时,我注意到cardview具有相同的选定微调器值,并且该复选框已被选中。例如,说我首先有一个带有“土豆”的卡片视图,然后在其上打勾。现在屏幕上什么也没有。然后我按fab添加cardview。然后,我看到cardview上带有wlel的“ potatoes”,并且勾选了该复选框。.如果那没有道理,请要求澄清

该怎么办?

ProductAdapter.java

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

    private Map<Integer, Integer> mSpinnerSelectedItem = new HashMap<Integer, Integer>();


//this context we will use to inflate the layout
    //Remove this..Please
    // CheckBox checkBox;

    //private Context mCtx;
    private SearchableSpinner spinner;

    //we are storing all the products in a list
    private List<Product> productList;

    private Activity create;

    public ProductAdapter(Activity activity) {
        create = activity;
    }


    //getting the context and product list with constructor
    public ProductAdapter(Activity activity, List<Product> productList) {
        // this.mCtx = mCtx;
        create = activity;
        this.productList = productList;
    }

    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //inflating and returning our view holder
        LayoutInflater inflater = LayoutInflater.from(create);
        View view = inflater.inflate(R.layout.layout_products, null);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ProductViewHolder holder, final int position) {
        // //getting the product of the specified position


        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(create, R.layout.item_spinner_layout,
                Product.getSpinnerItemsList());
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        holder.spinner.setAdapter(spinnerArrayAdapter);

        holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int mPosition, long id) {
                mSpinnerSelectedItem.put(position, mPosition);

                TextView mTextView = view.findViewById(R.id.mSpinnerText);
           /* Toast.makeText(create, "Selected Item: " + mTextView.getText().toString(), Toast.LENGTH_LONG).show();
            Log.e("***************", "Selected Item: " + mTextView.getText().toString());*/


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        //binding the data with the viewholder views
        if (mSpinnerSelectedItem.containsKey(position)) {
            holder.spinner.setSelection(mSpinnerSelectedItem.get(position));
        }


        holder.getView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(create);


                // set title
                alertDialogBuilder.setTitle("Delete Item");

                // set dialog message
                alertDialogBuilder
                        .setMessage("Are you sure you want to delete this item?")
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // if this button is clicked, close
                                // current activity


                                holder.checkBox.setChecked(false);
                                holder.spinner.setSelection(0);

                                productList.remove(position);
                                notifyItemRemoved(position);

                                Toast.makeText(create, "Item removed.", Toast.LENGTH_LONG).show();


                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

            }
        });





    }


    @Override
    public int getItemCount() {
        return productList.size();
    }


    class ProductViewHolder extends RecyclerView.ViewHolder {

        SearchableSpinner spinner;
        EditText editText;
        TextView textView5;
        CheckBox checkBox;
        LinearLayout linearLayout;
        View rootView;


        public ProductViewHolder(View itemView) {
            super(itemView);

            spinner = itemView.findViewById(R.id.spinner);
            editText = itemView.findViewById(R.id.editText);
            textView5 = itemView.findViewById(R.id.textView5);
            checkBox = itemView.findViewById(R.id.checkBox);
            rootView = itemView.findViewById(R.id.linearLayout);


            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // makes the set disappear when checkbox is ticked.
                    if(isChecked){

                        holder.checkBox.setChecked(false);
                        holder.spinner.setSelection(0);

                        productList.remove(getAdapterPosition());
                        notifyItemRemoved(getAdapterPosition());



                        Toast.makeText(create, "Done!", Toast.LENGTH_LONG).show();
                    }

                }
            });



        }

        public View getView() {
            return rootView;
        }

    }

}

1 个答案:

答案 0 :(得分:2)

RecyclerView回收ViewHolders的性能。 这意味着,如果在微调器中选择了“ ViewHolder A”,则即使您从列表中删除该项目,它也不会被破坏。当您将新项目添加到列表中时,您的RecyclerView可能会对该项目重复使用“ ViewHolder A”,并且选择的内容与上一个项目相同(删除之前)。

为避免这种情况,请在使用findViewByPosition的{​​{1}}方法将新项目添加到列表中时手动更新UI。

示例:

LayoutManager

如果您想将所有// FAB button onClick method: public void onClick(View v) { // Add item to RecyclerView list int itemPosition = myRecyclerViewAdapter.itemList.indexOf(newItem); ProductViewHolder vh = ((ProductViewHolder)recyclerView.getLayoutManager().findViewByPosition(itemPosition)); // Here, set everything to the default values (unchecked checkbox, etc) } 相关代码保留在适配器中(我鼓励您这样做),则可以在删除项目后将RecyclerView UI更新为默认值(而不是添加一个)时,您的ViewHolder就很新鲜,可以用于下一项了。

示例(更好地使用此解决方案):

ViewHolder