Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Add support for partial vs full items when searching for first visibl…
Browse files Browse the repository at this point in the history
…e item
  • Loading branch information
ShamylZakariya committed Jul 25, 2016
1 parent f6552c9 commit aa96bb6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Adapter and LayoutManager for Android RecyclerView which enables sticky header p
## Download
minSdkVersion: 11
```
compile 'org.zakariya.stickyheaders:stickyheaders:0.7.2'
compile 'org.zakariya.stickyheaders:stickyheaders:0.7.3'
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class DemoActivity extends AppCompatActivity {
private static final String TAG = DemoActivity.class.getSimpleName();
private static final String STATE_SCROLL_POSITION = "DemoActivity.STATE_SCROLL_POSITION";

public static final boolean SHOW_ADAPTER_POSITIONS = false;
public static final boolean SHOW_ADAPTER_POSITIONS = true;

RecyclerView recyclerView;
ProgressBar progressBar;
Expand Down Expand Up @@ -112,10 +112,11 @@ private void showInspectDialog() {
return;
}

boolean fullVisibleOnly = true;
StickyHeaderLayoutManager layoutManager = (StickyHeaderLayoutManager) recyclerView.getLayoutManager();
SectioningAdapter.HeaderViewHolder headerViewHolder = layoutManager.getFirstVisibleHeaderViewHolder();
SectioningAdapter.ItemViewHolder itemViewHolder = layoutManager.getFirstVisibleItemViewHolder();
SectioningAdapter.FooterViewHolder footerViewHolder = layoutManager.getFirstVisibleFooterViewHolder();
SectioningAdapter.HeaderViewHolder headerViewHolder = layoutManager.getFirstVisibleHeaderViewHolder(fullVisibleOnly);
SectioningAdapter.ItemViewHolder itemViewHolder = layoutManager.getFirstVisibleItemViewHolder(fullVisibleOnly);
SectioningAdapter.FooterViewHolder footerViewHolder = layoutManager.getFirstVisibleFooterViewHolder(fullVisibleOnly);

ArrayList<String> inspections = new ArrayList<>();

Expand Down
2 changes: 1 addition & 1 deletion stickyheaders/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.dcendents.android-maven'

group = 'org.zakariya.stickyheaders'
version = '0.7.2'
version = '0.7.3'

android {
compileSdkVersion 23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public Parcelable onSaveInstanceState() {
}

// Check if we're detached; if not, update
if(adapter != null)
if (adapter != null)
updateFirstAdapterPosition();
SavedState state = new SavedState();
state.firstViewAdapterPosition = firstViewAdapterPosition;
Expand All @@ -142,10 +142,10 @@ public void onRestoreInstanceState(Parcelable state) {
}

if (state instanceof SavedState) {
pendingSavedState = (SavedState)state;
pendingSavedState = (SavedState) state;
requestLayout();
} else {
Log.e(TAG, "onRestoreInstanceState: invalid saved state class, expected: " + SavedState.class.getCanonicalName() + " got: " + state.getClass().getCanonicalName() );
Log.e(TAG, "onRestoreInstanceState: invalid saved state class, expected: " + SavedState.class.getCanonicalName() + " got: " + state.getClass().getCanonicalName());
}
}

Expand Down Expand Up @@ -178,7 +178,7 @@ public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State
int totalVendedHeight = 0;

// If we emptied the view with a notify, we may overshoot and fail to draw
if(firstViewAdapterPosition > state.getItemCount())
if (firstViewAdapterPosition > state.getItemCount())
firstViewAdapterPosition = 0;

// walk through adapter starting at firstViewAdapterPosition stacking each vended item
Expand Down Expand Up @@ -320,7 +320,7 @@ public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerVi
isHeader = itemViewType == SectioningAdapter.TYPE_HEADER;

// If it's still a header, we don't need to do anything right now
if(isHeader)
if (isHeader)
break;
}

Expand Down Expand Up @@ -444,31 +444,34 @@ public void scrollToPosition(int position) {
}

/**
* @param fullyVisibleOnly if true, the search will be limited to the first item not hanging off top of screen or partially obscured by a header
* @return the viewholder for the first visible item (not header or footer)
*/
@Nullable
public SectioningAdapter.ItemViewHolder getFirstVisibleItemViewHolder() {
return (SectioningAdapter.ItemViewHolder) getFirstVisibleViewHolderOfType(SectioningAdapter.TYPE_ITEM);
public SectioningAdapter.ItemViewHolder getFirstVisibleItemViewHolder(boolean fullyVisibleOnly) {
return (SectioningAdapter.ItemViewHolder) getFirstVisibleViewHolderOfType(SectioningAdapter.TYPE_ITEM, fullyVisibleOnly);
}

/**
* @param fullyVisibleOnly if true, the search will be limited to the first header not hanging off top of screen
* @return the viewholder for the first visible header (not item or footer)
*/
@Nullable
public SectioningAdapter.HeaderViewHolder getFirstVisibleHeaderViewHolder() {
return (SectioningAdapter.HeaderViewHolder) getFirstVisibleViewHolderOfType(SectioningAdapter.TYPE_HEADER);
public SectioningAdapter.HeaderViewHolder getFirstVisibleHeaderViewHolder(boolean fullyVisibleOnly) {
return (SectioningAdapter.HeaderViewHolder) getFirstVisibleViewHolderOfType(SectioningAdapter.TYPE_HEADER, fullyVisibleOnly);
}

/**
* @param fullyVisibleOnly if true, the search will be limited to the first footer not hanging off top of screen or partially obscured by a header
* @return the viewholder for the first visible footer (not header or item)
*/
@Nullable
public SectioningAdapter.FooterViewHolder getFirstVisibleFooterViewHolder() {
return (SectioningAdapter.FooterViewHolder) getFirstVisibleViewHolderOfType(SectioningAdapter.TYPE_FOOTER);
public SectioningAdapter.FooterViewHolder getFirstVisibleFooterViewHolder(boolean fullyVisibleOnly) {
return (SectioningAdapter.FooterViewHolder) getFirstVisibleViewHolderOfType(SectioningAdapter.TYPE_FOOTER, fullyVisibleOnly);
}

@Nullable
SectioningAdapter.ViewHolder getFirstVisibleViewHolderOfType(int baseType) {
SectioningAdapter.ViewHolder getFirstVisibleViewHolderOfType(int baseType, boolean fullyVisibleOnly) {
if (getChildCount() == 0) {
return null;
}
Expand All @@ -478,7 +481,7 @@ SectioningAdapter.ViewHolder getFirstVisibleViewHolderOfType(int baseType) {
// our items is below this value
int firstHeaderBottom = 0;
if (baseType != SectioningAdapter.TYPE_HEADER) {
SectioningAdapter.HeaderViewHolder firstHeader = getFirstVisibleHeaderViewHolder();
SectioningAdapter.HeaderViewHolder firstHeader = getFirstVisibleHeaderViewHolder(false);
if (firstHeader != null) {
firstHeaderBottom = getDecoratedBottom(firstHeader.itemView);
}
Expand All @@ -501,13 +504,20 @@ SectioningAdapter.ViewHolder getFirstVisibleViewHolderOfType(int baseType) {
continue;
}

// filter out items which are fully obscured by a header
// filter out items which are partially or fully obscured by a header
int t = getDecoratedTop(v);
int b = getDecoratedBottom(v);
if (b <= firstHeaderBottom + 1) {
continue;

if (fullyVisibleOnly) {
if (t < firstHeaderBottom) {
continue;
}
} else {
if (b <= firstHeaderBottom + 1) {
continue;
}
}

int t = getDecoratedTop(v);
if (t < top) {
top = t;
topmostView = v;
Expand Down

0 comments on commit aa96bb6

Please sign in to comment.