Skip to content

Commit

Permalink
fix bug (VisibilityPercentsCalculator)
Browse files Browse the repository at this point in the history
  • Loading branch information
waynell committed Jul 15, 2016
1 parent 4401ac4 commit 1b01090
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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:
Expand All @@ -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){
Expand All @@ -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){
Expand Down Expand Up @@ -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();
Expand All @@ -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()){

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.graphics.Rect;
import android.view.View;

import com.waynell.videolist.visibility.items.ListItem;

/**
* @author Wayne
*/
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 1b01090

Please sign in to comment.