From 1b0109094827cfdb93c1bdd28f288a26bc9a6043 Mon Sep 17 00:00:00 2001 From: Wayne Date: Sat, 16 Jul 2016 00:47:11 +0800 Subject: [PATCH] fix bug (VisibilityPercentsCalculator) --- .../SingleListViewItemActiveCalculator.java | 33 ++++++++++++------- .../VisibilityPercentsCalculator.java | 10 +++++- 2 files changed, 30 insertions(+), 13 deletions(-) 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 053df49..c8dfd6a 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 @@ -28,7 +28,7 @@ public class SingleListViewItemActiveCalculator extends BaseItemsVisibilityCalculator { private static final String TAG = "ListViewItemActiveCal"; - private static final boolean SHOW_LOGS = true; + private static final boolean SHOW_LOGS = false; private static final int INACTIVE_LIST_ITEM_VISIBILITY_PERCENTS = 70; @@ -76,8 +76,11 @@ protected void onStateTouchScroll(ItemsPositionGetter itemsPositionGetter) { if(mCurrentItem.isVisibleItemChanged() && !mPreActiveItem.equals(mCurrentItem)) { if(SHOW_LOGS) { - Log.d(TAG, "onStateTouchScroll " + mCurrentItem + " " + VisibilityPercentsCalculator.getVisibilityPercents(mCurrentItem.getView()) - + " " + mPreActiveItem + " " + VisibilityPercentsCalculator.getVisibilityPercents(mPreActiveItem.getView())); + Log.d(TAG, "onStateTouchScroll " + mCurrentItem + " " + + VisibilityPercentsCalculator.getVisibilityPercents(mCurrentItem.getView(), + mCurrentItem.getListItem()) + " " + mPreActiveItem + " " + + VisibilityPercentsCalculator.getVisibilityPercents(mPreActiveItem.getView(), + mPreActiveItem.getListItem())); } mCallback.deactivateCurrentItem(mPreActiveItem.getListItem(), mPreActiveItem.getView(), @@ -160,8 +163,11 @@ public void onScrollStateIdle() { if (mCurrentItem.isVisibleItemChanged() && !mPreActiveItem.equals(mCurrentItem)) { if(SHOW_LOGS) { - Log.d(TAG, "onScrollStateIdle " + mCurrentItem + " " + VisibilityPercentsCalculator.getVisibilityPercents(mCurrentItem.getView()) - + " " + mPreActiveItem + " " + VisibilityPercentsCalculator.getVisibilityPercents(mPreActiveItem.getView())); + Log.d(TAG, "onScrollStateIdle " + mCurrentItem + " " + + VisibilityPercentsCalculator.getVisibilityPercents(mCurrentItem.getView(), + mCurrentItem.getListItem()) + " " + mPreActiveItem + " " + + VisibilityPercentsCalculator.getVisibilityPercents(mPreActiveItem.getView(), + mPreActiveItem.getListItem())); } if (mPreActiveItem.isAvailable()) { mCallback.deactivateCurrentItem(mPreActiveItem.getListItem(), @@ -182,7 +188,8 @@ public void onScrollStateIdle() { private void calculateMostVisibleItem(ItemsPositionGetter itemsPositionGetter, int firstVisiblePosition, int lastVisiblePosition) { ListItemData mostVisibleItem = getMockCurrentItem(itemsPositionGetter, firstVisiblePosition, lastVisiblePosition); - int maxVisibilityPercents = VisibilityPercentsCalculator.getVisibilityPercents(mostVisibleItem.getView()); + int maxVisibilityPercents = VisibilityPercentsCalculator + .getVisibilityPercents(mostVisibleItem.getView(), mostVisibleItem.getListItem()); switch (mScrollDirection){ case UP: @@ -206,12 +213,12 @@ private void topToBottomMostVisibleItem(ItemsPositionGetter itemsPositionGetter, int currentItemVisibilityPercents; for(int indexOfCurrentItem = itemsPositionGetter.getFirstVisiblePosition(), indexOfCurrentView = itemsPositionGetter.indexOfChild(outMostVisibleItem.getView()) - ; indexOfCurrentView < itemsPositionGetter.getChildCount() // iterating via listView Items + ; indexOfCurrentView < itemsPositionGetter.getChildCount() // iterating via listView Items ; indexOfCurrentItem++, indexOfCurrentView++){ ListItem listItem = mItemsProvider.getListItem(indexOfCurrentItem); View currentView = itemsPositionGetter.getChildAt(indexOfCurrentView); - currentItemVisibilityPercents = VisibilityPercentsCalculator.getVisibilityPercents(currentView); + currentItemVisibilityPercents = VisibilityPercentsCalculator.getVisibilityPercents(currentView, listItem); if(currentItemVisibilityPercents > mostVisibleItemVisibilityPercents && currentItemVisibilityPercents > INACTIVE_LIST_ITEM_VISIBILITY_PERCENTS){ @@ -232,12 +239,12 @@ private void bottomToTopMostVisibleItem(ItemsPositionGetter itemsPositionGetter, int currentItemVisibilityPercents; for(int indexOfCurrentItem = itemsPositionGetter.getLastVisiblePosition(), indexOfCurrentView = itemsPositionGetter.indexOfChild(outMostVisibleItem.getView()) - ; indexOfCurrentView >= 0 // iterating via listView Items + ; indexOfCurrentView >= 0 // iterating via listView Items ; indexOfCurrentItem--, indexOfCurrentView--){ ListItem listItem = mItemsProvider.getListItem(indexOfCurrentItem); View currentView = itemsPositionGetter.getChildAt(indexOfCurrentView); - currentItemVisibilityPercents = VisibilityPercentsCalculator.getVisibilityPercents(currentView); + currentItemVisibilityPercents = VisibilityPercentsCalculator.getVisibilityPercents(currentView, listItem); if(currentItemVisibilityPercents > mostVisibleItemVisibilityPercents && currentItemVisibilityPercents > INACTIVE_LIST_ITEM_VISIBILITY_PERCENTS){ @@ -296,7 +303,8 @@ private ListItemData getMockCurrentItem(ItemsPositionGetter itemsPositionGetter, */ private void calculateActiveItem(ItemsPositionGetter itemsPositionGetter, ListItemData listItemData) { /** 1. */ - int currentItemVisibilityPercents = VisibilityPercentsCalculator.getVisibilityPercents(listItemData.getView()); + int currentItemVisibilityPercents = VisibilityPercentsCalculator + .getVisibilityPercents(listItemData.getView(), listItemData.getListItem()); /** 2. */ ListItemData neighbourItemData = new ListItemData(); @@ -310,7 +318,8 @@ private void calculateActiveItem(ItemsPositionGetter itemsPositionGetter, ListIt } /** 3. */ - int nextItemVisibilityPercents = VisibilityPercentsCalculator.getVisibilityPercents(neighbourItemData.getView()); + int nextItemVisibilityPercents = VisibilityPercentsCalculator + .getVisibilityPercents(neighbourItemData.getView(), neighbourItemData.getListItem()); if(enoughPercentsForDeactivation(currentItemVisibilityPercents, nextItemVisibilityPercents) && neighbourItemData.isAvailable()){ 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 637fa3f..91cd9c5 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 @@ -3,6 +3,8 @@ import android.graphics.Rect; import android.view.View; +import com.waynell.videolist.visibility.items.ListItem; + /** * @author Wayne */ @@ -14,9 +16,10 @@ public class VisibilityPercentsCalculator { * Note: visibility doesn't have to depend on the visibility of a full view. * It might be calculated by calculating the visibility of any inner View * + * @param item * @return percents of visibility */ - public static int getVisibilityPercents(View view) { + public static int getVisibilityPercents(View view, ListItem item) { final Rect currentViewRect = new Rect(); int percents = 100; @@ -36,6 +39,11 @@ public static int getVisibilityPercents(View view) { percents = currentViewRect.bottom * 100 / height; } + // only ListItem's visibility could be 100 percent + if (item == null && percents == 100) { + percents--; + } + return percents; }