From 70c88d64841494089bf6c91a0813ebf09f149895 Mon Sep 17 00:00:00 2001 From: Davide Steduto Date: Sun, 29 Apr 2018 23:15:57 +0200 Subject: [PATCH] Fixed #594 - Updating sticky header checks same item view type and increased delay to 100ms, giving more time to the LayoutManager computation. --- .../flexibleadapter/FlexibleAdapter.java | 22 ++++++++++++------- .../helpers/StickyHeaderHelper.java | 18 ++++++++++----- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/flexible-adapter/src/main/java/eu/davidea/flexibleadapter/FlexibleAdapter.java b/flexible-adapter/src/main/java/eu/davidea/flexibleadapter/FlexibleAdapter.java index f072fe55..23c933b6 100644 --- a/flexible-adapter/src/main/java/eu/davidea/flexibleadapter/FlexibleAdapter.java +++ b/flexible-adapter/src/main/java/eu/davidea/flexibleadapter/FlexibleAdapter.java @@ -2509,7 +2509,7 @@ public int expand(T item) { /** * Convenience method to initially expand a single item. Parent won't be notified. - *

Note: Must be used in combination with adding new items that require to be + *

Note: Must be used in combination when adding new items, those require to be * initially expanded.

* WARNING! *
Expanded status is ignored if {@code init = true}: it will always attempt to expand @@ -2550,7 +2550,7 @@ private int expand(int position, boolean expandAll, boolean init, boolean notify IExpandable expandable = (IExpandable) item; if (!hasSubItems(expandable)) { - expandable.setExpanded(false);//clear the expanded flag + expandable.setExpanded(false); // Clear the expanded flag log.w("No subItems to Expand on position %s expanded %s", position, expandable.isExpanded()); return 0; } @@ -4452,7 +4452,7 @@ public long getTime() { private void initializeItemTouchHelper() { if (mItemTouchHelper == null) { if (mRecyclerView == null) { - throw new IllegalStateException("RecyclerView cannot be null. Enabling LongPressDrag or Swipe must be done after the Adapter is added to the RecyclerView."); + throw new IllegalStateException("RecyclerView cannot be null. Enabling LongPressDrag or Swipe must be done after the Adapter has been attached to the RecyclerView."); } if (mItemTouchHelperCallback == null) { mItemTouchHelperCallback = new ItemTouchHelperCallback(this); @@ -4520,10 +4520,11 @@ public final boolean isLongPressDragEnabled() { } /** - * Enable / Disable the Drag on LongPress on the entire ViewHolder. + * Enables / Disables the drag of the item when long-press the entire itemView. *

Note: This will skip LongClick on the view in order to handle the LongPress, * however the LongClick listener will be called if necessary in the new * {@link FlexibleViewHolder#onActionStateChanged(int, int)}.

+ * Requires the Adapter being attached to the RecyclerView. * Default value is {@code false}. * * @param longPressDragEnabled true to activate, false otherwise @@ -4553,7 +4554,8 @@ public final boolean isHandleDragEnabled() { } /** - * Enable / Disable the drag of the itemView with a handle view. + * Enables / Disables the drag of the item with a handle view. + * Requires the Adapter being attached to the RecyclerView. *

Default value is {@code false}.

* * @param handleDragEnabled true to activate, false otherwise @@ -4582,8 +4584,12 @@ public final boolean isSwipeEnabled() { } /** - * Enable the Full Swipe of the items. - *

Default value is {@code false}.

+ * Enables full the swipe of the items. + *

Note:

+ * + * Default value is {@code false}. * * @param swipeEnabled true to activate, false otherwise * @return this Adapter, so the call can be chained @@ -5285,7 +5291,7 @@ private void updateStickyHeader(int positionStart) { public void run() { if (areHeadersSticky()) mStickyHeaderHelper.updateOrClearHeader(true); } - }, 50L); + }, 100L); } } diff --git a/flexible-adapter/src/main/java/eu/davidea/flexibleadapter/helpers/StickyHeaderHelper.java b/flexible-adapter/src/main/java/eu/davidea/flexibleadapter/helpers/StickyHeaderHelper.java index 7c494f33..dc946e28 100644 --- a/flexible-adapter/src/main/java/eu/davidea/flexibleadapter/helpers/StickyHeaderHelper.java +++ b/flexible-adapter/src/main/java/eu/davidea/flexibleadapter/helpers/StickyHeaderHelper.java @@ -33,6 +33,8 @@ import eu.davidea.flexibleadapter.utils.Log; import eu.davidea.viewholders.FlexibleViewHolder; +import static eu.davidea.flexibleadapter.utils.LayoutUtils.getClassName; + /** * A sticky header helper, to use only with {@link FlexibleAdapter}. *

Header ViewHolders must be of type {@link FlexibleViewHolder}.

@@ -159,13 +161,18 @@ private void updateHeader(int headerPosition, boolean updateHeaderContent) { int oldHeaderPosition = mHeaderPosition; mHeaderPosition = headerPosition; FlexibleViewHolder holder = getHeaderViewHolder(headerPosition); - Log.d("swapHeader newHeaderPosition=%s", mHeaderPosition); + // Swapping header swapHeader(holder, oldHeaderPosition); } else if (updateHeaderContent) { - // #299 - ClassCastException after click on expanded sticky header when AutoCollapse is enabled -// mStickyHeaderViewHolder = getHeaderViewHolder(headerPosition); -// mStickyHeaderViewHolder.setBackupPosition(headerPosition); - mAdapter.onBindViewHolder(mStickyHeaderViewHolder, headerPosition); + // #299 - ClassCastException after click on expanded sticky header when auto-collapse is enabled. + // #594 - Checking same item view type and increased delay to 100ms. + if (mStickyHeaderViewHolder.getItemViewType() == mAdapter.getItemViewType(headerPosition)) { + mAdapter.onBindViewHolder(mStickyHeaderViewHolder, headerPosition); + } else { + Log.e("updateHeader Wrong itemViewType for StickyViewHolder=%s, PositionViewHolder=%s", + getClassName(mStickyHeaderViewHolder), + getClassName(getHeaderViewHolder(headerPosition))); + } ensureHeaderParent(); } translateHeader(); @@ -233,6 +240,7 @@ private void translateHeader() { } private void swapHeader(FlexibleViewHolder newHeader, int oldHeaderPosition) { + Log.d("swapHeader newHeaderPosition=%s", mHeaderPosition); if (mStickyHeaderViewHolder != null) { resetHeader(mStickyHeaderViewHolder); // #568, #575 - Header ViewHolder out of the top screen must be recycled manually