代码之家  ›  专栏  ›  技术社区  ›  JRowan

从片段添加到recyclerview,从recyclerview内部从recyclerview中删除

  •  0
  • JRowan  · 技术社区  · 7 年前

    大家好,我有一个片段,我可以毫无问题地将所有元素添加到recyclerview中。如果我从recyclerview中删除,一切都很好。但在我删除了一些东西之后,我从适配器列表中删除的项目似乎并没有从我传递到适配器的片段列表中删除,因为如果我添加其他内容,所有删除的项目都会返回到recyclerview。我已经记录了列表的大小,这是正确的,只是如果我删除一个项目或多个项目,当我向回收器添加一个元素时,它会添加所有其他删除的项目,但它似乎只算作一个,因为如果我点击删除任何旧的项目,它们都会再次消失,我已经做了一段时间,我找不到解决方案,谢谢你的时间

    下面是片段中的相关代码

     public void instantiateViews(){
            View power1 = (View)getView().findViewById(R.id.include);
            View mega1 = (View)getView().findViewById(R.id.include1);
            powerBall = (TextView)power1.findViewById(R.id.win);
            megaMillions = (TextView)mega1.findViewById(R.id.winm);
            power = (RecyclerView)getView().findViewById(R.id.powerRecycle);
            mega = (RecyclerView)getView().findViewById(R.id.megaRecycle);
        }
        public void getMyTickets(){
            powerNumbers = SharedPrefHelper.getMyTickets(getActivity(),"powerball");
            megaNumbers = SharedPrefHelper.getMyTickets(getActivity(),"megamillions");
        }
        public void setMyTickets(){
            SharedPrefHelper.setMyTickets(getActivity(),powerNumbers,"powerball");
            SharedPrefHelper.setMyTickets(getActivity(),megaNumbers,"megamillions");
        }
        public void populateRecyclerViews(){
            //power.setHasFixedSize(true);
            LinearLayoutManager llm = new LinearLayoutManager(getActivity());
            power.setLayoutManager(llm);
            powerAdapter = new MyTicketAdapter(powerNumbers,"powerball");
            power.setAdapter(powerAdapter);
    
            //mega.setHasFixedSize(true);
            LinearLayoutManager llm1 = new LinearLayoutManager(getActivity());
            mega.setLayoutManager(llm1);
            megaAdapter = new MyTicketAdapter(megaNumbers,"megamillions");
            mega.setAdapter(megaAdapter);
        }
        public void setClickListeners(){
            powerBall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
    // ...Irrelevant code for customizing the buttons and title
                    LayoutInflater inflater = getActivity().getLayoutInflater();
                    final View dialogView = inflater.inflate(R.layout.ticket_selector, null);
                    final NumberPicker one = (NumberPicker)dialogView.findViewById(R.id.one);
                    one.setMinValue(1);
                    one.setMaxValue(69);
                    final NumberPicker two = (NumberPicker)dialogView.findViewById(R.id.two);
                    two.setMinValue(1);
                    two.setMaxValue(69);
                    final NumberPicker three = (NumberPicker)dialogView.findViewById(R.id.three);
                    three.setMinValue(1);
                    three.setMaxValue(69);
                    final NumberPicker four = (NumberPicker)dialogView.findViewById(R.id.four);
                    four.setMinValue(1);
                    four.setMaxValue(69);
                    final NumberPicker five = (NumberPicker)dialogView.findViewById(R.id.five);
                    five.setMinValue(1);
                    five.setMaxValue(69);
                    final NumberPicker six = (NumberPicker)dialogView.findViewById(R.id.six);
                    six.setMinValue(1);
                    six.setMaxValue(26);
                    final RadioGroup group = (RadioGroup)dialogView.findViewById(R.id.group);
    
                    dialogBuilder.setView(dialogView);
    
    
    
    
                    final AlertDialog alertDialog = dialogBuilder.create();
                    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", null, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            alertDialog.dismiss();
                        }
                    });
                    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Add", null, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //powerNumbers = SharedPrefHelper.getMyTickets(getActivity(),"powerball");
                            //powerNumbers = powerAdapter.mDataset;
                            String ticket = one.getValue() + " " + two.getValue() + " " + three.getValue() +
                                    " " + four.getValue() + " " + five.getValue() + " " + six.getValue();
                            int mul = group.getCheckedRadioButtonId();
                            RadioButton but = dialogView.findViewById(mul);
                            String multi = but.getText().toString();
                            MyTicket ticket1 = new MyTicket();
                            ticket1.ticket = ticket;
                            ticket1.multi = multi;
                            powerNumbers.add(ticket1);
                            Log.d("adapter",powerNumbers.size()+"");
                            powerAdapter.notifyItemInserted(powerNumbers.size() - 1);
                            SharedPrefHelper.setMyTickets(getActivity(),powerNumbers,"powerball");
                            alertDialog.dismiss();
    
                        }
                    });
                    alertDialog.show();
                }
            });
            megaMillions.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
    // ...Irrelevant code for customizing the buttons and title
                    LayoutInflater inflater = getActivity().getLayoutInflater();
                    final View dialogView = inflater.inflate(R.layout.ticket_selector, null);
                    final NumberPicker one = (NumberPicker)dialogView.findViewById(R.id.one);
                    one.setMinValue(1);
                    one.setMaxValue(69);
                    final NumberPicker two = (NumberPicker)dialogView.findViewById(R.id.two);
                    two.setMinValue(1);
                    two.setMaxValue(69);
                    final NumberPicker three = (NumberPicker)dialogView.findViewById(R.id.three);
                    three.setMinValue(1);
                    three.setMaxValue(69);
                    final NumberPicker four = (NumberPicker)dialogView.findViewById(R.id.four);
                    four.setMinValue(1);
                    four.setMaxValue(69);
                    final NumberPicker five = (NumberPicker)dialogView.findViewById(R.id.five);
                    five.setMinValue(1);
                    five.setMaxValue(69);
                    final NumberPicker six = (NumberPicker)dialogView.findViewById(R.id.six);
                    six.setMinValue(1);
                    six.setMaxValue(26);
                    six.setBackgroundColor(Color.YELLOW);
                    final RadioGroup group = (RadioGroup)dialogView.findViewById(R.id.group);
    
                    dialogBuilder.setView(dialogView);
    
    
    
    
                    final AlertDialog alertDialog = dialogBuilder.create();
                    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", null, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            alertDialog.dismiss();
                        }
                    });
                    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Add", null, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //megaNumbers.clear();
                            //megaNumbers = SharedPrefHelper.getMyTickets(getActivity(),"megamillions");
                           // megaNumbers = new ArrayList<MyTicket>(megaAdapter.mDataset);
                            String ticket = one.getValue() + " " + two.getValue() + " " + three.getValue() +
                                    " " + four.getValue() + " " + five.getValue() + " " + six.getValue();
                            int mul = group.getCheckedRadioButtonId();
                            RadioButton but = dialogView.findViewById(mul);
                            String multi = but.getText().toString();
                            MyTicket ticket1 = new MyTicket();
                            ticket1.ticket = ticket;
                            ticket1.multi = multi;
                            megaNumbers.add(ticket1);
                            megaAdapter.notifyItemInserted(megaNumbers.size() - 1);
                            //megaAdapter.notifyDataSetChanged();
                            SharedPrefHelper.setMyTickets(getActivity(),megaNumbers,"megamillions");
                            alertDialog.dismiss();
    
                        }
                    });
                    alertDialog.show();
                }
            });
        }
    

    这是我的整个recyclerview适配器

    public class MyTicketAdapter extends RecyclerView.Adapter<MyTicketAdapter.MyViewHolder> {
        public ArrayList<MyTicket> mDataset;
        private String type;
    
    
        // Provide a reference to the views for each data item
        // Complex data items may need more than one view per item, and
        // you provide access to all the views for a data item in a view holder
        public static class MyViewHolder extends RecyclerView.ViewHolder {
            // each data item is just a string in this case
            CardView cv;
            LinearLayout linear;
            Context context;
            public MyViewHolder(View itemView, Context context) {
                super(itemView);
                cv = (CardView) itemView.findViewById(R.id.cv);
                linear = (LinearLayout) itemView.findViewById(R.id.linear);
                this.context = context;
            }
        }
    
        // Provide a suitable constructor (depends on the kind of dataset)
        public MyTicketAdapter(ArrayList<MyTicket> myDataset, String type) {
            mDataset = myDataset;
            this.type = type;
    
        }
    
        // Create new views (invoked by the layout manager)
        @Override
        public MyTicketAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                         int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
            MyViewHolder vh = new MyViewHolder(v,parent.getContext());
            return vh;
        }
    
        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {
            if(type.equals("powerball")){
                final View view;
                LayoutInflater inflater = (LayoutInflater)   holder.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.balls, null);
                TextView ball1 = (TextView) view.findViewById(R.id.ball1);
                TextView ball2 = (TextView) view.findViewById(R.id.ball2);
                TextView ball3 = (TextView) view.findViewById(R.id.ball3);
                TextView ball4 = (TextView) view.findViewById(R.id.ball4);
                TextView ball5 = (TextView) view.findViewById(R.id.ball5);
                TextView ball6 = (TextView) view.findViewById(R.id.ball6);
                TextView multi = (TextView) view.findViewById(R.id.multi);
                multi.setText("Multiplier: " + mDataset.get(position).multi);
                TextView win = (TextView)view.findViewById(R.id.win);
                win.setText(holder.context.getResources().getString(R.string.remove));
    
                TextView[] images = {ball1,ball2,ball3,ball4,ball5,ball6};
                //String num = mDataset.get(position).ticket;
                String num = mDataset.get(position).ticket;
                String[] split = num.split(" ");
                for(int j = 0;j < split.length;j++){
                    images[j].setText(split[j]);
                }
                holder.linear.addView(view);
                win.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //holder.linear.removeView(view);
                        //mDataset.remove(holder.getAdapterPosition());
                        mDataset.remove(holder.getAdapterPosition());
                        Log.d("adapter",mDataset.size()+"");
                        notifyItemRemoved(holder.getAdapterPosition());
                        notifyItemRangeChanged(holder.getAdapterPosition(), getItemCount());
                        //notifyDataSetChanged();
    
                        SharedPrefHelper.setMyTickets(holder.context,mDataset,"powerball");
                    }
                });
            }else{
                View view;
                LayoutInflater inflater = (LayoutInflater)   holder.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.balls1, null);
                TextView ball1 = (TextView) view.findViewById(R.id.ball1m);
                TextView ball2 = (TextView) view.findViewById(R.id.ball2m);
                TextView ball3 = (TextView) view.findViewById(R.id.ball3m);
                TextView ball4 = (TextView) view.findViewById(R.id.ball4m);
                TextView ball5 = (TextView) view.findViewById(R.id.ball5m);
                TextView ball6 = (TextView) view.findViewById(R.id.ball6m);
                TextView multi = (TextView) view.findViewById(R.id.multim);
                multi.setText("Multiplier: " + mDataset.get(position).multi);
                TextView win = (TextView)view.findViewById(R.id.winm);
                win.setText(holder.context.getResources().getString(R.string.remove));
                TextView[] images = {ball1,ball2,ball3,ball4,ball5,ball6};
                String num = mDataset.get(position).ticket;
                String[] split = num.split(" ");
                for(int j = 0;j < split.length;j++){
                    images[j].setText(split[j]);
                }
                holder.linear.addView(view);
                win.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //holder.linear.removeView(view);
                        mDataset.remove(holder.getAdapterPosition());
    
                        notifyItemRemoved(holder.getAdapterPosition());
                        notifyItemRangeChanged(holder.getAdapterPosition(), 1);
                        //notifyDataSetChanged();
    
                        SharedPrefHelper.setMyTickets(holder.context,mDataset,"megamillions");
                    }
                });
            }
    
        }
    
        // Return the size of your dataset (invoked by the layout manager)
        @Override
        public int getItemCount() {
            return mDataset.size();
        }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Serg Burlaka    7 年前

    可能需要从回收站移除视图

    recycler.removeViewAt(position);
    

    this

    但我认为,如果您在从适配器获取回调调用时尝试从adaper中更改列表,可能会使您的逻辑更加清晰。

    我是说在科特林:

     interface RemoveListern{
          fun onRemove()
        }
    
         class Adapter(val listenr:RemoveListener)...
    
    
     on Activity:
    
              val adapter = Adapter(object:RemoveListener{
    
               oveeride fun onRemove(){
                    //<----  MODIFY HERE
                    list.remove(position);
                    recycler.removeViewAt(position);
                    mAdapter.notifyItemRemoved(position);                 
                    mAdapter.notifyItemRangeChanged(position, list.size())
               }
    })