Skip to content

Commit

Permalink
fix ListItemData.getIndex() NPE issue
Browse files Browse the repository at this point in the history
  • Loading branch information
waynell committed Jun 3, 2016
1 parent b921453 commit 1b549e1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,22 @@ protected void onStateTouchScroll(ItemsPositionGetter itemsPositionGetter) {
* @param outNextItemData - out parameter. It will be filled with next item data if the one exists
*/
private void findNextItem(ItemsPositionGetter itemsPositionGetter, ListItemData currentIem, ListItemData outNextItemData) {
int nextItemIndex = currentIem.getIndex() + 1;
if(currentIem.isIndexValid()) {
int nextItemIndex = currentIem.getIndex() + 1;

if(nextItemIndex < mItemsProvider.listItemSize()){
int indexOfCurrentView = itemsPositionGetter.indexOfChild(currentIem.getView());
if (nextItemIndex < mItemsProvider.listItemSize()) {
int indexOfCurrentView = itemsPositionGetter.indexOfChild(currentIem.getView());

if(indexOfCurrentView >= 0){
View nextView = itemsPositionGetter.getChildAt(indexOfCurrentView + 1);
if(nextView != null){
ListItem next = mItemsProvider.getListItem(nextItemIndex);
if (next == null && SHOW_LOGS) {
Log.e(TAG, "null list item");
}
if (indexOfCurrentView >= 0) {
View nextView = itemsPositionGetter.getChildAt(indexOfCurrentView + 1);
if (nextView != null) {
ListItem next = mItemsProvider.getListItem(nextItemIndex);
if (next == null && SHOW_LOGS) {
Log.e(TAG, "null list item");
}

outNextItemData.fillWithData(nextItemIndex, nextView, next);
outNextItemData.fillWithData(nextItemIndex, nextView, next);
}
}
}
}
Expand All @@ -129,19 +131,21 @@ private void findNextItem(ItemsPositionGetter itemsPositionGetter, ListItemData
* @param outPreviousItemData - out parameter. It will be filled with previous item data if the one exists
*/
private void findPreviousItem(ItemsPositionGetter itemsPositionGetter, ListItemData currentIem, ListItemData outPreviousItemData) {
int previousItemIndex = currentIem.getIndex() -1;
if(currentIem.isIndexValid()) {
int previousItemIndex = currentIem.getIndex() - 1;

if(previousItemIndex >= 0){
int indexOfCurrentView = itemsPositionGetter.indexOfChild(currentIem.getView());
if (previousItemIndex >= 0) {
int indexOfCurrentView = itemsPositionGetter.indexOfChild(currentIem.getView());

if(indexOfCurrentView > 0){
View previousView = itemsPositionGetter.getChildAt(indexOfCurrentView - 1);
ListItem previous = mItemsProvider.getListItem(previousItemIndex);
if (previous == null && SHOW_LOGS) {
Log.e(TAG, "null list item");
}
if (indexOfCurrentView > 0) {
View previousView = itemsPositionGetter.getChildAt(indexOfCurrentView - 1);
ListItem previous = mItemsProvider.getListItem(previousItemIndex);
if (previous == null && SHOW_LOGS) {
Log.e(TAG, "null list item");
}

outPreviousItemData.fillWithData(previousItemIndex, previousView, previous);
outPreviousItemData.fillWithData(previousItemIndex, previousView, previous);
}
}
}
}
Expand Down Expand Up @@ -240,11 +244,11 @@ private void bottomToTopMostVisibleItem(ItemsPositionGetter itemsPositionGetter,
mostVisibleItemVisibilityPercents = currentItemVisibilityPercents;
outMostVisibleItem.fillWithData(indexOfCurrentItem, currentView, listItem);
}
}

boolean itemChanged = !mCurrentItem.equals(outMostVisibleItem);
boolean itemChanged = !mCurrentItem.equals(outMostVisibleItem);

outMostVisibleItem.setVisibleItemChanged(itemChanged);
}
outMostVisibleItem.setVisibleItemChanged(itemChanged);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ public static int getVisibilityPercents(View view) {
return 0;
}

view.getLocalVisibleRect(currentViewRect);
if(view.getLocalVisibleRect(currentViewRect)) {

if(viewIsPartiallyHiddenTop(currentViewRect)){
// view is partially hidden behind the top edge
percents = (height - currentViewRect.top) * 100 / height;
} else if(viewIsPartiallyHiddenBottom(currentViewRect, height)){
percents = currentViewRect.bottom * 100 / height;
if (viewIsPartiallyHiddenTop(currentViewRect)) {
// view is partially hidden behind the top edge
percents = (height - currentViewRect.top) * 100 / height;
} else if (viewIsPartiallyHiddenBottom(currentViewRect, height)) {
percents = currentViewRect.bottom * 100 / height;
}

return percents;
}

return percents;
return 0;
}

private static boolean viewIsPartiallyHiddenBottom(Rect currentViewRect, int height) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public ListItemData fillWithData(int indexInAdapter, View view, ListItem item) {
return this;
}

public boolean isIndexValid() {
return mIndexInAdapter != null;
}

public boolean isAvailable() {
return mIndexInAdapter != null && mView != null && mListItem != null;
}
Expand Down

0 comments on commit 1b549e1

Please sign in to comment.