diff --git a/video-list-player/src/main/java/com/waynell/videolist/visibility/calculator/SingleListViewItemActiveCalculator.java b/video-list-player/src/main/java/com/waynell/videolist/visibility/calculator/SingleListViewItemActiveCalculator.java index 9a5ce77..053df49 100644 --- a/video-list-player/src/main/java/com/waynell/videolist/visibility/calculator/SingleListViewItemActiveCalculator.java +++ b/video-list-player/src/main/java/com/waynell/videolist/visibility/calculator/SingleListViewItemActiveCalculator.java @@ -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); + } } } } @@ -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); + } } } } @@ -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); } /** diff --git a/video-list-player/src/main/java/com/waynell/videolist/visibility/calculator/VisibilityPercentsCalculator.java b/video-list-player/src/main/java/com/waynell/videolist/visibility/calculator/VisibilityPercentsCalculator.java index 96ee8b7..637fa3f 100644 --- a/video-list-player/src/main/java/com/waynell/videolist/visibility/calculator/VisibilityPercentsCalculator.java +++ b/video-list-player/src/main/java/com/waynell/videolist/visibility/calculator/VisibilityPercentsCalculator.java @@ -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) { diff --git a/video-list-player/src/main/java/com/waynell/videolist/visibility/items/ListItemData.java b/video-list-player/src/main/java/com/waynell/videolist/visibility/items/ListItemData.java index c04aace..439cc8d 100644 --- a/video-list-player/src/main/java/com/waynell/videolist/visibility/items/ListItemData.java +++ b/video-list-player/src/main/java/com/waynell/videolist/visibility/items/ListItemData.java @@ -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; }