简体   繁体   English

从片段添加到recyclerview并从recyclerview内部从recyclerview删除

[英]adding to recyclerview from fragment and deleting from recyclerview from inside recyclerview

hello everyone I have a fragment and I can add all of the elements into the recyclerview with no problem. 大家好,我有一个片段,我可以毫无问题地将所有元素添加到recyclerview中。 Then if I delete from inside the recyclerview everything is fine. 然后,如果我从recyclerview内部删除,一切都很好。 but then after I delete something it seems that the item that I deleted from the adapters list isn't deleted from the fragments list that I passed into the adapter because if I add something else all of the deleted items go back into the recyclerview. 但是然后,在我删除某些内容之后,似乎从适配器列表中删除的项目并未从传递到适配器中的片段列表中删除,因为如果添加其他内容,所有已删除的项目都将返回到recyclerview中。 I have logged the size of the list and it is correct its just that if I delete an item or multiple items when I add 1 element to the recycler it adds all of the other deleted ones but it seems that it only counts as one because if I click delete on any of the old ones they all disappear again, I have been at this for a while and I cannot find the solution, thank you for your time 我已经记录了列表的大小,这是正确的,只是当我在回收站中添加1个元素时删除一个或多个项目时,它会添加所有其他已删除的项目,但似乎仅算作一个,因为如果我单击任何旧的删除按钮,它们又都消失了,我已经有一段时间了,但找不到解决方法,谢谢您的时间

here is the relevant code from the fragment 这是片段中的相关代码

 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();
            }
        });
    }

and here is my whole recyclerview adapter 这是我的整个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();
    }

Maybe need remove view from recycler 也许需要从回收站中删除视图

recycler.removeViewAt(position);

looks this 看起来这个

But i think if you try change list in out of adaper on get callbacks invokation from adapter , maybe you logic becamy more crleary . 但是我认为,如果您尝试从适配器获取回调调用时,不使用adaper更改列表,也许您的逻辑会变得更加脆弱。

I mean on Kotlin: 我是说Kotlin:

 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())
           }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM