Skip to content

Commit

Permalink
Merge pull request #309 from elfman/master
Browse files Browse the repository at this point in the history
Expand and collapse can be done with animation or not.
  • Loading branch information
CymChad authored Sep 9, 2016
2 parents acf211c + 800c0b5 commit 011fc4f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private ArrayList<MultiItemEntity> generateData() {
for (int i = 0; i < lv0Count; i++) {
Level0Item lv0 = new Level0Item("This is " + i + "th item in Level 0", "subtitle of " + i);
for (int j = 0; j < lv1Count; j++) {
Level1Item lv1 = new Level1Item("Level 1 item: " + j, "just subtitle");
Level1Item lv1 = new Level1Item("Level 1 item: " + j, "(no animation)");
for (int k = 0; k < personCount; k++) {
lv1.addSubItem(new Person(nameList[k], random.nextInt(40)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public void onClick(View v) {
int pos = holder.getAdapterPosition();
Log.d(TAG, "Level 1 item pos: " + pos);
if (lv1.isExpanded()) {
collapse(pos);
collapse(pos, false);
} else {
expand(pos);
expand(pos, false);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,9 +973,11 @@ private int recursiveExpand(int position, @NonNull List list) {
* Expand an expandable item
*
* @param position position of the item
* @param animate expand items with animation
* @param shouldNotify notify the RecyclerView to rebind items, <strong>false</strong> if you want to do it yourself.
* @return the number of items that have been added.
*/
public int expand(@IntRange(from = 0) int position) {
public int expand(@IntRange(from = 0) int position, boolean animate, boolean shouldNotify) {
position -= getHeaderLayoutCount();

T item = getItem(position);
Expand All @@ -997,10 +999,39 @@ public int expand(@IntRange(from = 0) int position) {
expandable.setExpanded(true);
subItemCount += list.size();
}
notifyItemRangeInserted(position + 1 + getHeaderLayoutCount(), subItemCount);
int parentPos = position + getHeaderLayoutCount();
if (shouldNotify) {
if (animate) {
notifyItemChanged(parentPos);
notifyItemRangeInserted(parentPos + 1, subItemCount);
} else {
notifyDataSetChanged();
}
}
return subItemCount;
}

/**
* Expand an expandable item
*
* @param position position of the item
* @param animate expand items with animation
* @return the number of items that have been added.
*/
public int expand(@IntRange(from = 0) int position, boolean animate) {
return expand(position, animate, true);
}

/**
* Expand an expandable item with animation.
*
* @param position position of the item
* @return the number of items that have been added.
*/
public int expand(@IntRange(from = 0) int position) {
return expand(position, true, true);
}

private int recursiveCollapse(@IntRange(from = 0) int position) {
T item = getItem(position);
if (!isExpandable(item)) {
Expand Down Expand Up @@ -1032,7 +1063,7 @@ private int recursiveCollapse(@IntRange(from = 0) int position) {
* @param position the position of the item
* @return the number of subItems collapsed.
*/
public int collapse(@IntRange(from = 0) int position) {
public int collapse(@IntRange(from = 0) int position, boolean animate) {
position -= getHeaderLayoutCount();
T item = getItem(position);
if (!isExpandable(item)) {
Expand All @@ -1041,10 +1072,20 @@ public int collapse(@IntRange(from = 0) int position) {
IExpandable expandable = (IExpandable) item;
int subItemCount = recursiveCollapse(position);
expandable.setExpanded(false);
notifyItemRangeRemoved(position + 1 + getHeaderLayoutCount(), subItemCount);
int parentPos = position + getHeaderLayoutCount();
if (animate) {
notifyItemChanged(parentPos);
notifyItemRangeRemoved(parentPos + 1, subItemCount);
} else {
notifyDataSetChanged();
}
return subItemCount;
}

public int collapse(@IntRange(from = 0) int position) {
return collapse(position, true);
}

private int getItemPosition(T item) {
return item != null && mData != null && !mData.isEmpty() ? mData.indexOf(item) : -1;
}
Expand Down

0 comments on commit 011fc4f

Please sign in to comment.