From 800c0b59ed96d9cd44e2118878cdf5b0fb9d357f Mon Sep 17 00:00:00 2001 From: Harlan Date: Fri, 9 Sep 2016 08:58:35 +0800 Subject: [PATCH] Expand and collapse can be done with animation or not. --- .../ExpandableUseActivity.java | 2 +- .../adapter/ExpandableItemAdapter.java | 4 +- .../adapter/base/BaseQuickAdapter.java | 49 +++++++++++++++++-- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ExpandableUseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ExpandableUseActivity.java index 6a8b78715..6afc51610 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ExpandableUseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ExpandableUseActivity.java @@ -72,7 +72,7 @@ private ArrayList 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))); } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ExpandableItemAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ExpandableItemAdapter.java index d0934a254..5407a9304 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ExpandableItemAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ExpandableItemAdapter.java @@ -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); } } }); diff --git a/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.java b/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.java index fdd281f07..798dfea2e 100755 --- a/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.java +++ b/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.java @@ -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, false 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); @@ -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)) { @@ -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)) { @@ -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; }