1

I want to create a list of items using RecyclerView and want to expand particular item when clicked (Like in phone call list ). I want to achieve this without using any library. Can anyone help ?

1 Answers1

1

Get child data list as a Member of Parent data in dataset. And, at click event of RecyclerView row, use them like this.. here mdataSet is main dataset for RecyclerView

final TitleHolder holder = (TitleHolder) h;
        final Model model = (Model) mdataSet.get(position);

        holder.txt_title.setText(model.getTitle());
        holder.childItem = model;

        holder.txt_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (model.getChildList() == null) {
                    //collapse
                    ((Model) mdataSet.get(mdataSet.indexOf(holder.childItem))).isExpanded = false;
                    holder.arrow.startAnimation(AnimationUtils.loadAnimation(context, R.anim.arrow_reverse));
                    model.childList = new ArrayList<ModelData>();
                    int count = 0;
                    int pos = mdataSet.indexOf(holder.childItem);
                    while (mdataSet.size() > pos + 1 && mdataSet.get(pos + 1).type == Model.VIEW_CHILD) {
                        model.childList.add((ModelData) mdataSet.remove(pos + 1));
                        count++;
                    }
                    notifyItemRangeRemoved(pos + 1, count);
                } else {
                    //expand
                    ((Model) mdataSet.get(mdataSet.indexOf(holder.childItem))).isExpanded = true;
                    holder.arrow.startAnimation(AnimationUtils.loadAnimation(context, R.anim.arrow));
                    int pos = mdataSet.indexOf(holder.childItem);
                    int index = pos + 1;
                    for (ModelData i : model.getChildList()) {
                        mdataSet.add(index, i);
                        index++;
                    }
                    notifyItemRangeInserted(pos + 1, index - pos - 1);
                    model.childList = null;

                }

            }
        });

        if (((Model) mdataSet.get(mdataSet.indexOf(holder.childItem))).isExpanded) {
            holder.arrow.startAnimation(AnimationUtils.loadAnimation(context, R.anim.arrow));
        }

Here, I will add child data to Main dataset at click event on txt_title Again, use Title(parent) and data(child) as two different ViewTypes like this

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

        if (viewType == VIEW_TITLE) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_expand_title, parent, false);
            return new TitleHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.row_wallet_history, parent, false);
            return new DataHolder(itemView);
        }
    }

OR

If your child view is fix (which you want to expand/collapse) then wrap them inside layout and, make that Layout visible/Gone with animation in order to achieve expand collapse effect Refere this link to make them animated

Community
  • 1
  • 1
Milind Mevada
  • 3,145
  • 1
  • 14
  • 22