N
N
Neonoviiwolf2017-02-10 20:01:35
Java
Neonoviiwolf, 2017-02-10 20:01:35

Restoring content in CardView after rotation?

Good!
Cards are created, LinearLayout is added to them, LinearLayout has its own content. When the device is rotated, the cards are recreated naturally, I try to add the previously created LinearLayout to the new cards in order to restore the content. Accordingly, I run into an error that the LinearLayout already has a parent. Where and how can I remove a link to old cards in the adapter, or how is it better to implement it?

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

        if (AddItemMenu.getInstance().getLayoutArrayList().size() != data.size()) {
            holder.layout = new LinearLayout(holder.cardView.getContext());
            holder.layout.setId(position);
            AddItemMenu.getInstance().addCardItem(position);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            holder.layout.setOrientation(LinearLayout.VERTICAL);
            holder.layout.setLayoutParams(params);
            holder.cardView.addView(holder.layout);

            View viewPlus = new ItemPlus().onCreateView((LayoutInflater) holder.layout.getContext().
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE), holder.layout, new Bundle());
            holder.layout.addView(viewPlus);

            AddItemMenu.getInstance().getLayoutArrayList().add(holder.layout); //отдельный класс синглтон, в котором храню ArrayList<LinearLayout>
        } else {
            holder.cardView.addView(AddItemMenu.getInstance().getLayoutArrayList().get(position));//тут пытаюсь заполнить новые карточки данными
        }
    }

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

    public static class FineFoodHolder extends RecyclerView.ViewHolder {

        protected View view;
        private CardView cardView;
        private LinearLayout layout;

        public FineFoodHolder(final View itemView) {
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.card_view);

        }
    }

although probably even if (AddItemMenu.getInstance().getLayoutArrayList().size() != data.size()) will mess up on cards that do not fit on the screen, but the problem is not in it. How can one check if a given index exists in an array or if a try-catch current? After all, cards are created as they are displayed on the screen, respectively, if I only follow the number of layouts, it will not work correctly if the list is long and does not fit the whole screen, then after viewing half and returning to the top card, the top cards are recreated and in place with them layout - you

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question