Skip to content

Commit

Permalink
Optimized fix for pull request #116
Browse files Browse the repository at this point in the history
  • Loading branch information
davideas committed Jun 18, 2016
1 parent 8334d12 commit afa159a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:background="@color/material_color_deep_purple_400"
android:background="#dd7e57c2"
android:elevation="4dp">

<LinearLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:background="@color/material_color_deep_purple_400"
android:background="#dd7e57c2"
android:elevation="4dp">

<LinearLayout
Expand Down Expand Up @@ -35,7 +35,7 @@
android:maxLines="1"
android:textAppearance="@style/TextAppearance.AppCompat.Caption"
android:textColor="@color/material_text_color_white_text"
tools:text="Subtitle"/>
tools:text="Header Subtitle"/>

</LinearLayout>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package eu.davidea.flexibleadapter.helpers;

import android.animation.Animator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
Expand Down Expand Up @@ -109,10 +108,6 @@ public void onAnimationRepeat(Animator animation) {
}
}

// public int getCurrentStickyHeaderPosition() {
// return mHeaderPosition;
// }

private void initStickyHeadersHolder() {
//Initialize Holder Layout and show sticky header if exists already
mStickyHolderLayout = mAdapter.getStickySectionHeadersHolder();
Expand Down Expand Up @@ -167,22 +162,11 @@ private void translateHeader() {

int headerOffsetX = 0, headerOffsetY = 0;

//If linear Layout, next potential header is always at position 1
int nextChildHeaderPosition = 1;
if (mRecyclerView.getLayoutManager() instanceof GridLayoutManager) {
nextChildHeaderPosition = ((GridLayoutManager) mRecyclerView.getLayoutManager()).getSpanCount();
} else if (mRecyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
nextChildHeaderPosition = ((StaggeredGridLayoutManager) mRecyclerView.getLayoutManager()).getSpanCount();
}
//Search for the position where the next header item is found and take the new offset
for (int i = 0; i < mRecyclerView.getChildCount(); i++) {
final View nextChild = mRecyclerView.getChildAt(i);
if (nextChild != null) {
int adapterPos = mRecyclerView.getChildAdapterPosition(nextChild);
//noinspection unchecked
if (!mAdapter.isHeader(mAdapter.getItem(adapterPos)) && childIsSmallerThanHeader(nextChild)) {
continue;
}
int nextHeaderPosition = getHeaderPosition(adapterPos);
if (mHeaderPosition != nextHeaderPosition) {
if (getOrientation(mRecyclerView) == OrientationHelper.HORIZONTAL) {
Expand All @@ -209,13 +193,6 @@ private void translateHeader() {
//Log.v(TAG, "TranslationX=" + headerOffsetX + " TranslationY=" + headerOffsetY);
}

private boolean childIsSmallerThanHeader(View nextChild) {
if (getOrientation(mRecyclerView) == OrientationHelper.HORIZONTAL) {
return nextChild.getWidth() < mStickyHeaderViewHolder.itemView.getWidth();
}
return nextChild.getHeight() < mStickyHeaderViewHolder.itemView.getHeight();
}

private void swapHeader(FlexibleViewHolder newHeader) {
if (mStickyHeaderViewHolder != null) {
resetHeader(mStickyHeaderViewHolder);
Expand Down Expand Up @@ -256,10 +233,6 @@ private int getHeaderPosition(int adapterPosHere) {
adapterPosHere = mRecyclerView.getChildAdapterPosition(firstChild);
}
IHeader header = mAdapter.getSectionHeader(adapterPosHere);
//Header cannot be sticky if it's also an Expandable in collapsed status, RV will raise an exception
if (header == null || mAdapter.isExpandable(header) && !mAdapter.isExpanded(header)) {
return RecyclerView.NO_POSITION;
}
return mAdapter.getGlobalPositionOf(header);
}

Expand All @@ -268,8 +241,7 @@ private int getHeaderPosition(int adapterPosHere) {
* yet, it will be created, measured, and laid out.
*
* @param position the adapter position to get the header view for
* @return Header view or null if the associated position and previous has
* no header
* @return Header ViewHolder of the associated header position
*/
@SuppressWarnings("unchecked")
private FlexibleViewHolder getHeaderViewHolder(int position) {
Expand Down

0 comments on commit afa159a

Please sign in to comment.