javaandroidandroid-recyclerviewnestedrecyclerview

Removing an item from a nested RecyclerView


I've been duelling with this problem for a good few hours now. I have a nested RecyclerView (i.e. a RecyclerView that encompasses an inner Recycler view). Both the parent and child recycler view's are dynamic. The problem I encounter is that I cannot find a way to correctly notify the child (inner) recycler view when a CRUD, in particular a delete, occurs. At first it works ok, but then I get all sorts of random errors from "You must be a direct descend view" or getAdapterPosition returning -1 or just simply incorrect positions. I think my implementation is pretty standard so I ask what is the correct way to notify the inner recycler view.

I am pretty close to returning to my former implementation which involved an array of fragments each containing a recycling view, but I question about the performance of such design. My code is as follows:

Parent RecyclerView

public class RecipeRecyclerAdapter extends RecyclerView.Adapter<RecipeRecyclerAdapter.ViewHolder>
{
    public interface OnRecipeRecyclerListener
    {
        //--------------------------- Proxy methods for OnDishRecyclerListener -----------------

        void renameDish(int DishPosition, int RecipePosition);

        void deleteDish(int DishPosition, int RecipePosition);


        //--------------------------- OnRecipeRecyclerListener methods ----------------------------

        void deleteRecipe(int RecipePosition);

        void renameRecipe(int RecipePosition);

    }

    //Recycler Pool and tools
    private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();

    //Recycler Parameters
    private ArrayList<Recipe> allRecipes;
    private Context context;


    //Listener
    @Setter
    private OnRecipeRecyclerListener onRecipeRecyclerListener;


    public RecipeRecyclerAdapter(Context context, ArrayList<Recipe> allRecipes)
    {
        this.allRecipes = allRecipes;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_Recipe, parent, false);

        return new RecipeRecyclerAdapter.ViewHolder(view, onRecipeRecyclerListener, context);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position)
    {
        Recipe Recipe = allRecipes.get(position);  

        holder.RecipeName.setText(Utils.colourFirstLetter(context, Recipe.getRecipeName(), R.color.progressFxBar));
        holder.RecipeDate.setText(Utils.getDate(Recipe.getTimestamp()));

        // Create layout manager with initial prefetch item count
        LinearLayoutManager layoutManager = new LinearLayoutManager(
                holder.DishsRecycler.getContext(),
                LinearLayoutManager.VERTICAL,
                false
        );
        layoutManager.setInitialPrefetchItemCount(Recipe.getDishs().size());

        DishRecyclerAdapter DishsRecyclerAdapter = new DishRecyclerAdapter(Recipe.getDishs(), holder, context);        

        holder.DishsRecycler.setLayoutManager(layoutManager);
        holder.DishsRecycler.setAdapter(DishsRecyclerAdapter);
        holder.DishsRecycler.setRecycledViewPool(viewPool);
    }


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

    static class ViewHolder extends RecyclerView.ViewHolder implements DishRecyclerAdapter.OnDishRecyclerListener
        private OnRecipeRecyclerListener onRecipeRecyclerListener;
        private Context context;

        TextView RecipeName, RecipeDate;
        ImageView addDish;

        //The Dishs Recycler
        RecyclerView DishsRecycler;

        public ViewHolder(@NonNull View itemView, OnRecipeRecyclerListener onRecipeRecyclerListener, Context context)
        {
            super(itemView);

            this.onRecipeRecyclerListener = onRecipeRecyclerListener;
            this.context = context;

            RecipeName = itemView.findViewById(R.id.RecipeName);
            RecipeDate = itemView.findViewById(R.id.RecipeDate);           
            addDish = itemView.findViewById(R.id.addDish);

            DishsRecycler = itemView.findViewById(R.id.DishsRecyclerView);

            loadListeners(itemView);
        }

        private void loadListeners(@NonNull View initView)
        {
            RecipeName.setOnClickListener(v ->
            {
                PopupMenu popup = new PopupMenu(context, v);
                MenuInflater inflater = popup.getMenuInflater();
                inflater.inflate(R.menu.Recipe_floating_menu, popup.getMenu());
                popup.show();

                popup.setOnMenuItemClickListener(item ->
                {
                    switch (item.getItemId())
                    {
                        case R.id.menuDeleteRecipe:
                            onRecipeRecyclerListener.deleteRecipe(getAdapterPosition());

                            return true;
                        case R.id.menuRenameRecipe:
                            onRecipeRecyclerListener.renameRecipe(getAdapterPosition());

                            return true;
                        case R.id.menuRecipeProps:
                            onRecipeRecyclerListener.RecipeProps(getAdapterPosition());

                            return true;
                        default:
                            return false;
                    }
                });
            });

            addDish.setOnClickListener(v ->
            {
                onRecipeRecyclerListener.addDish(getAdapterPosition());
            });        

        }       


        //******************************* OnDishRecyclerListener *******************************

        @Override
        public void renameDish(int position)
        {
            onRecipeRecyclerListener.renameDish(position, getAdapterPosition());
        }

        @Override
        public void deleteDish(int position)
        {
            onRecipeRecyclerListener.deleteDish(position, getAdapterPosition());
        }
    }
}

Child (inner) RecyclerView

public class DishRecyclerAdapter extends RecyclerView.Adapter<DishRecyclerAdapter.ViewHolder>
{
    public interface OnDishRecyclerListener
    {
        void renameDish(int position);

        void deleteDish(int position);
    }

    private OnDishRecyclerListener onDishRecyclerListener;

    private ArrayList<Dish> allDishs;
    private Context context;

    public DishRecyclerAdapter(ArrayList<Dish> allDishs, OnDishRecyclerListener onDishRecyclerListener, Context context)
    {
        this.onDishRecyclerListener = onDishRecyclerListener;
        this.allDishs = allDishs;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_Dishs, parent, false);

        return new ViewHolder(context, view, onDishRecyclerListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position)
    {
        Dish Dish = allDishs.get(position);

        holder.DishName.setText(Dish.getDishName());
    }


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

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        private Context context;      
        TextView DishName; //plus a bunch of other Views I just removed for the sake of simplicity  

        OnDishRecyclerListener onDishRecyclerListener;

        public ViewHolder(Context context, @NonNull View itemView, OnDishRecyclerListener onDishRecyclerListener)
        {
            super(itemView);
            this.context = context;

            DishName = itemView.findViewById(R.id.DishName);

            this.onDishRecyclerListener = onDishRecyclerListener;

            loadListeners(itemView);
        }

        private void loadListeners(@NonNull View v)
        {
            //Rename an Dish
            DishName.setOnClickListener(view ->
            {
                PopupMenu popup = new PopupMenu(context, v);
                MenuInflater inflater = popup.getMenuInflater();
                inflater.inflate(R.menu.Dish_floating_menu, popup.getMenu());
                popup.show();

                popup.setOnMenuItemClickListener(item ->
                {
                    switch (item.getItemId())
                    {
                        case R.id.menuDeleteDish:
                            onDishRecyclerListener.deleteDish(getAdapterPosition());

                            return true;
                        case R.id.menuRenameDish:
                            onDishRecyclerListener.renameDish(getAdapterPosition());

                            return true;
                        case R.id.menuDishProps:

                            return true;
                        default:
                            return false;
                    }
                });
            });         
        }
    }
}

An extraction of the fragment calling the parent recycler view:

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_Recipe_panel, container, false);

        recyclerRecipe = view.findViewById(R.id.RecipeRecyclerView);

        SimpleItemAnimator simpleItemAnimator = (SimpleItemAnimator) recyclerRecipe.getItemAnimator();

        if(simpleItemAnimator !=null)
        {
            simpleItemAnimator.setSupportsChangeAnimations(true);
        }

        RecipeAdapter = new RecipeRecyclerAdapter(getContext(), allRecipes);
        RecipeAdapter.setOnRecipeRecyclerListener(this);

        //recyclerRecipe.setHasFixedSize(true);
        recyclerRecipe.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerRecipe.setAdapter(RecipeAdapter);

        return view;
    }

   public void createRecipe(String RecipeName)
    {
        Recipe Recipe = new Recipe(RecipeName, getContext());
        allRecipes.add(0,Recipe);
        RecipeAdapter.notifyItemInserted(0);
    }

    @Override
    public void deleteRecipe(int RecipePosition)
    {
        allRecipes.remove(RecipePosition);
        RecipeAdapter.notifyItemRemoved(RecipePosition);
    }


    @Override
    public void addDish(int RecipePosition)
    {

      allRecipes.get(RecipePosition).getDishs().add(new Dish(DishName));
      RecipeAdapter.notifyItemChanged(RecipePosition);

    }

    @Override
    public void deleteDish(int DishPosition, int RecipePosition)
    {
        Recipe Recipe = allRecipes.get(RecipePosition);
        Dish Dish = Recipe.getDishs().get(DishPosition);

        Dish.getTimer().destroyTimer();
        Recipe.getDishs().remove(DishPosition);
        RecipeAdapter.notifyItemChanged(RecipePosition);
    }

Solution

  • I figured out what the problem was (after LOADS OF HOURS). I needed to notify first the parent recycler and then the child recycler in that order.

    //adding an item to the inner list
    recipeAdapter.notifyItemChanged(recipePosition);            
    dishsRecycler.getAdapter().notifyItemInserted(recipe.getDishs().size()-1); 
    
    //deleting an inner list item
    recipeAdapter.notifyItemChanged(recipePosition);            
    dishsRecycler.getAdapter().notifyItemRemoved()
    

    However the biggest culprit was having a common recyclerPool for all the inner recyclerviews, so removed this line from the code

    //REMOVED THESE LINES
    private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
    holder.DishsRecycler.setRecycledViewPool(viewPool);
    

    Also, I refrained from using notifyDataSet() as that for some reason throws NO_POSITION (-1).