Представление вложенного переработчика, дочерний и родительский элементы устраняют проблему

#java #android #android-studio #android-recyclerview

#java #Android #android-студия #android-recyclerview

Вопрос:

Я создал вложенный recycle rview, в котором родительский и дочерний элементы являются динамическими.Дополнение как к родительскому, так и к дочернему элементу работает нормально.Удаление из дочернего элемента работает нормально.Я хочу удалить родительский вид, когда все дочерние виды для одного и того же будут удалены.Сбит с толку тем, как я мог бы реализовать то же самое.

Вот дочерний адаптер

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<ToDoModel> values;
    private Context context;
    //private OnEditListener onEditListener;
    AlertDialog alertDialog;

    /*public MyAdapter(List<ToDoModel> context) {
        this.values = context;
        this.onEditListener = onEditListener;
    }*/


    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView txtHeader;
        public TextView txtFooter;
        public ImageView mIcon;
        public View layout;
        public ImageView deleteItem;
        public CheckBox doneButton;
        public ImageView editItem;

        public ViewHolder(View v) {
            super(v);
            layout = v;
            txtHeader = (TextView) v.findViewById(R.id.firstLine);
            txtFooter = (TextView) v.findViewById(R.id.secondLine);
            mIcon = (ImageView) v.findViewById(R.id.icon);
            deleteItem = (ImageView) v.findViewById(R.id.deleteButton);
            doneButton = (CheckBox) v.findViewById(R.id.checkBox);
            editItem = (ImageView) v.findViewById(R.id.editButton);
        }
    }

    public void add(int position, ToDoModel item) {
        values.add(position, item);
        sort();
        notifyDataSetChanged();
        //notifyItemInserted(position);
    }

    public void replace(ToDoModel item)
    {
        int editPos;
        editPos = values.indexOf(item);
        values.set(editPos,item);
        sort();
        notifyDataSetChanged();
    }

    public void sort() {
        if ((int) values.size() > 1) {
            Collections.sort(values);
        }
    }

    public void remove(int position) {
        values.remove(position);
        notifyDataSetChanged();
    }

    public MyAdapter(List<ToDoModel> myDataset) {
        values = myDataset;
    }


    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {

        LayoutInflater inflater = LayoutInflater.from(
                parent.getContext());
        View v =
                inflater.inflate(R.layout.todo_row_view, parent, false);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        final ToDoModel toDoModel = values.get(position);

        switch ((String) toDoModel.task) {
            case "To Go":
                holder.mIcon.setImageResource(R.drawable.ic_explore24px);
                break;
            case "To Get":
                holder.mIcon.setImageResource(R.drawable.ic_shoppingbag24px);
                break;
            case "To Do":
                holder.mIcon.setImageResource(R.drawable.ic_todo);
                break;
            default:
                break;

        }

        holder.txtHeader.setText(toDoModel.header);
        holder.txtFooter.setText(toDoModel.task   " | "   toDoModel.tod   " | "   toDoModel.footer);

        holder.doneButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isChecked()) {
                    //holder.mIcon.setImageResource(R.drawable.ic_check_circle24px);
                    Toast.makeText(buttonView.getContext(), "Marked As Done", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(buttonView.getContext(), "Marked As UnDone", Toast.LENGTH_SHORT).show();
                }
            }
        });


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

                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                View view = LayoutInflater.from(v.getContext()).inflate(R.layout.delete_promptdialog,null);
                Button YesButton = (Button) view.findViewById(R.id.buttonYes);
                Button NoButton = (Button) view.findViewById(R.id.buttonNo);

                YesButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        remove(position);
                        Toast.makeText(v.getContext(), "List Item Removed", Toast.LENGTH_SHORT).show();
                        alertDialog.dismiss();
                    }
                });
                NoButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(v.getContext(), "Delete Canceled", Toast.LENGTH_SHORT).show();
                        alertDialog.dismiss();
                    }
                });

                builder.setView(view);
                alertDialog=builder.create();
                alertDialog.show();
                /*remove(position);
                Toast.makeText(v.getContext(), "List Item Removed", Toast.LENGTH_SHORT).show();*/
            }
        });


       /* holder.editItem.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                onEditListener.onEditClick(values.get(position));
                //Toast.makeText(v.getContext(), "List Edit", Toast.LENGTH_SHORT).show();
            }
        });*/

    }

   /*public interface OnEditListener {

        void onEditClick(ToDoModel currentData);

    }*/


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

}
 

Вот родительский адаптер

 public class ParentAdapter extends RecyclerView.Adapter<ParentAdapter.ParentViewHolder> {

    private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();

    public List<TodoParent> parentList;
    public TodoParent todayList;
    public static long todayEpochDate;

    public ParentAdapter(List<TodoParent> parentList, MainFragment mainFragment) {

        this.parentList = parentList;

    }

    public void add(int position, TodoParent item) {
        parentList.add(position, item);
        sort();
        notifyDataSetChanged();
        //notifyItemInserted(position);
    }
    public void sort() {
        if ((int) parentList.size() > 1) {
            Collections.sort(parentList);
            //if(!(todayList.ParentTitle.isEmpty())) Collections.swap(parentList, parentList.indexOf(todayList), 0);
        }

       /* if ( todayList )
        {
            System.out.println("Entering Swap");
            setToday(todayList);
        }*/
    }

    public void setToday(TodoParent item) {
        int todayPos;
        todayPos = parentList.indexOf(item);
        parentList.remove(todayPos);
        notifyDataSetChanged();
        parentList.add(0,item);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ParentAdapter.ParentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.todo_parent_view,parent,false);
        ParentViewHolder vh =  new ParentViewHolder(view);
        return vh;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(@NonNull ParentViewHolder holder, int position) {

        TodoParent todoParent = parentList.get(position);
        todoParent.ParentId = position;

        Calendar mCal;
        mCal = Calendar.getInstance();
        int mDay = mCal.get(Calendar.DAY_OF_MONTH);
        int mMonth = mCal.get(Calendar.MONTH);
        int mYear = mCal.get(Calendar.YEAR);
        
        String dateString = mDay  "/"  (mMonth 1)  "/"   mYear;
        try {
            todayEpochDate = new SimpleDateFormat("dd/MM/yyyy").parse(dateString).getTime()/1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if((long)todayEpochDate == (long)todoParent.EpochTime)
        {
            holder.mTitle.setText("Today");
            todayList = todoParent;
            System.out.println("todayE:" todayEpochDate);
            System.out.println("SelectedE:" todoParent.EpochTime);
        }
        else
        {
            holder.mTitle.setText(todoParent.ParentTitle);
            System.out.println("today:" todayEpochDate);
            System.out.println("Selected:" todoParent.EpochTime);
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(holder.childView.getContext(),
                LinearLayoutManager.VERTICAL,false);
        layoutManager.setInitialPrefetchItemCount(todoParent.mChildList.size());

        holder.childView.setLayoutManager(layoutManager);

        MyAdapter myAdapter = new MyAdapter(todoParent.mChildList);

        holder.childView.setAdapter(myAdapter);
        holder.childView.setRecycledViewPool(viewPool);

    }

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

    public class ParentViewHolder extends RecyclerView.ViewHolder {

        public TextView mTitle;
        public RecyclerView childView;

        public ParentViewHolder(@NonNull View itemView) {
            super(itemView);
            mTitle = itemView.findViewById(R.id.mParentTitle);
            childView = itemView.findViewById(R.id.mChildView);
        }
    }
}
 

удаление для ребенка работает нормально. Как удалить родительский элемент здесь, когда дочерних представлений больше нет?