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 ?
Asked
Active
Viewed 3,372 times
1
-
withour libraary you can't do it cause android does provide expanddable recycler view.. here is good tutorial for expandable recycler view https://www.bignerdranch.com/blog/expand-a-recyclerview-in-four-steps/ – Abhishek Singh Feb 09 '17 at 10:47
-
@AbhishekSingh you can do that without external library. see my ans – Milind Mevada Feb 09 '17 at 10:54
-
@MilindMevada thanks i will try that. – Abhishek Singh Feb 09 '17 at 10:59
1 Answers
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
-
yes. this is another way to achieve this. thanks @AbhishekSingh for merge my ans. I've deleted my other post – Milind Mevada Feb 10 '17 at 04:53
-