Home > Mobile >  How to add dynamic views to android layout in recyclerview adapter
How to add dynamic views to android layout in recyclerview adapter

Time:12-09

enter image description here

I have a list of parents that each has zero or many children I use recyclerview to show the parents. I want to show parents as header of each recyclerview item and below show all the children what is the best way to show the children? is it suitable to show children in a listview or add dynamic views for each child if so how to do that? I have attach the image of layout, please check what I mean thanks

enter image description here

CodePudding user response:

I think, may this helpful bcz, in every position there are dynamicly generate item view respectably.

                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
                IAFTbind.rcvFilters.setLayoutManager(linearLayoutManager);
                IAFTbind.rcvFilters.setAdapter(new RecyclerView.Adapter() {
                    public int checkedPosition = 0;


                    @NonNull
                    @Override
                    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        ImageView imageView = new ImageView(getContext());
                        imageView.setScaleX(.9f);
                        imageView.setScaleY(.9f);
                        imageView.setLayoutParams(new ViewGroup.LayoutParams(size, size));
                        return new RecyclerView.ViewHolder(imageView) {
                            @Override
                            public String toString() {
                                return super.toString();
                            }
                        };
                    }

                    @Override
                    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                        int p = holder.getAdapterPosition();

                        ((ImageView) holder.itemView).setBackground(new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(fBits.get(p), size, size, false)));

//                        ((ImageView) holder.itemView).setBackground(new BitmapDrawable(getResources(), bitmaps.get(position)));

                        int cp = this.checkedPosition;

                        if (cp == -1) {
                            ((ImageView) holder.itemView).setImageResource(0);
                        } else if (cp == p) {
                            ((ImageView) holder.itemView).setImageResource(R.drawable.draw_tool_selecter_strock);
                        } else {
                            ((ImageView) holder.itemView).setImageResource(0);
                        }


                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ((ImageView) holder.itemView).setImageResource(R.drawable.draw_tool_selecter_strock);
                                checkedPosition = p;
                                notifyDataSetChanged();

                                changeBitmapEffect(p);
                            }
                        });
                    }

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

                });

CodePudding user response:

Problem statement

  • You want to add childs dynamically as per the screenshot you shared.

Solution

So here I can see 2 recycler views, one inside another. The first recycler view is for parents which have 2 items

  • Parents name
  • Another recycler view for children

What you need to do is, you need to pass the updated list of item to the parent's recyclerview adapter, which will send the updated list of item to childs recyclerview adapter and the childs recyclerview adapter will check using the diffUtil about any changes and it will reflect the changes in the recyclerview.

  • Related