Skip to content

Commit

Permalink
@emir-hasanbegovic [#69447912] started handling padding
Browse files Browse the repository at this point in the history
  • Loading branch information
emir-hasanbegovic authored and Emir Hasanbegovic committed Apr 21, 2014
1 parent 6bce373 commit 8f363e8
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 48 deletions.
Expand Up @@ -261,30 +261,34 @@ private boolean needLayout(final int size, final int scrollDisplacement) {
final Cell firstPosition = getFirstCell();
final Cell lastPosition = getLastCell();

final boolean firstAndLastNotOnScreen = firstPosition == null && lastPosition == null;
if (firstAndLastNotOnScreen) {
final boolean firstPositionOnScreen = firstPosition == null;
final boolean lastPositionOnScreen = lastPosition == null;
if (!firstPositionOnScreen && !lastPositionOnScreen) {
/**
* Assumption is that if first and last item are not on screen
* then the rest of the items have been layout out correctly.
* Note that we have already covered the case where there are no views
* on screen.
*/

return false;
}

if (firstPosition != null) {
final int displacement = mSnapPositionInterface.getCellDisplacementFromSnapPosition(this, size, firstPosition);
if (displacement < 0) {
mStartCellPosition = 0;
mOffset += displacement;
return true;
}
final int displacement = mSnapPositionInterface.getDisplacementFromSnapPosition(this, size, firstPosition, lastPosition);
if (displacement == 0) {
return false;
}

if (lastPosition != null) {
final int displacement = mSnapPositionInterface.getCellDisplacementFromSnapPosition(this, size, lastPosition);
if (displacement > 0) {
mStartCellPosition = getCellCount() - 1;
mOffset += displacement;
return true;
}
mOffset += displacement;
if (firstPositionOnScreen) {
mStartCellPosition = 0;
} else if (firstPositionOnScreen) {
mStartCellPosition = getCellCount() - 1;
} else {

}

return false;
return true;

}

Expand Down Expand Up @@ -387,7 +391,8 @@ public int getCellSizeTotal() {
return viewSizeTotal + cellSpacingCount * cellSpacing;
}

private int getOverDrawAdjust(final boolean isCircularScroll, final int size, final int displacement) {
private int getOverDrawAdjust(final boolean isCircularScroll, final int size,
final int displacement) {
final boolean viewsBeingDrawn = !mCells.isEmpty();
if (!viewsBeingDrawn || isCircularScroll) return 0;

Expand Down Expand Up @@ -504,7 +509,8 @@ public int getAdapterCount() {
/**
* When moving left, every time a view is removed, this means that we are removing the leftMost view and therefore have to increment the mOffset by the removed view's width
*/
private void layoutCells(final AdapterViewHandler adapterViewHandler, final int size, final int breadth) {
private void layoutCells(final AdapterViewHandler adapterViewHandler, final int size,
final int breadth) {
final int startSizePadding = getStartSizePadding();
final int endSizePadding = getEndSizePadding();
mLayoutCellCount = 0;
Expand Down Expand Up @@ -820,7 +826,8 @@ public boolean setSelected(final View view) {
return mSelectedPositionManager.setSelectedPosition(position);
}

private void jumpToPosition(final AdapterViewHandler adapterViewHandler, final int position) {
private void jumpToPosition(final AdapterViewHandler adapterViewHandler,
final int position) {
final int size = mScrollDirectionManager.getViewGroupSize(mViewGroup);

final View nearestViewToSnapPosition = getNearestViewToSnapPosition(size);
Expand Down Expand Up @@ -867,7 +874,8 @@ protected Move getMove(final int displacement) {
return Move.none;
}

public void setAnimationStoppedListener(final AnimationStoppedListener animationStoppedListener) {
public void setAnimationStoppedListener(
final AnimationStoppedListener animationStoppedListener) {
mAnimationStoppedListener = animationStoppedListener;
}

Expand Down
Expand Up @@ -28,7 +28,29 @@ public int getDrawLimitMoveBackwardOverDrawAdjust(LayoutManager<Cell> layoutMana
}

@Override
public int getCellDisplacementFromSnapPosition(LayoutManager<Cell> layoutManager, int size, Cell cell) {
public int getDisplacementFromSnapPosition(LayoutManager<Cell> layoutManager, int size, Cell firstPosition, Cell lastPosition) {
final Integer firstDisplacement = getCellDisplacementFromSnapPosition(layoutManager, size, firstPosition);
final Integer lastDisplacement = getCellDisplacementFromSnapPosition(layoutManager, size, lastPosition);

if (firstDisplacement != null && lastDisplacement != null) {
if (Math.abs(firstDisplacement) < Math.abs(lastDisplacement)) {
return firstDisplacement;
} else {
return lastDisplacement;
}
} else if (firstDisplacement != null) {
return firstDisplacement;
} else if (lastDisplacement != null) {
return lastDisplacement;
}

return 0;
}

private Integer getCellDisplacementFromSnapPosition(LayoutManager<Cell> layoutManager, int size, Cell cell) {
if (cell == null) {
return null;
}
final int startSizePadding = layoutManager.getStartSizePadding();
final int currentCellCenter = layoutManager.getCellCenter(cell);
final int center = startSizePadding + size / 2;
Expand All @@ -38,7 +60,7 @@ public int getCellDisplacementFromSnapPosition(LayoutManager<Cell> layoutManager

@Override
public int getCellDistanceFromSnapPosition(final LayoutManager<Cell> layoutManager, final int size, final Cell cell) {
final int displacement = getCellDisplacementFromSnapPosition(layoutManager, size, cell);
final int displacement = getDisplacementFromSnapPosition(layoutManager, size, cell, null);
return Math.abs(displacement);
}

Expand Down
Expand Up @@ -27,7 +27,20 @@ public int getDrawLimitMoveBackwardOverDrawAdjust(LayoutManager<Cell> layoutMana
}

@Override
public int getCellDisplacementFromSnapPosition(LayoutManager<Cell> layoutManager, int size, Cell cell) {
public int getDisplacementFromSnapPosition(LayoutManager<Cell> layoutManager, int size, Cell firstPosition, Cell lastPosition) {
final Integer lastDisplacement = getCellDisplacementFromSnapPosition(layoutManager, size, lastPosition);

if (lastDisplacement != null) {
return lastDisplacement;
}

return 0;
}

private Integer getCellDisplacementFromSnapPosition(LayoutManager<Cell> layoutManager, int size, Cell cell) {
if (cell == null) {
return null;
}
final int currentCellEnd = layoutManager.getCellEnd(cell);
final int startSizePadding = layoutManager.getStartSizePadding();
final int displacement = startSizePadding + size - currentCellEnd;
Expand Down
Expand Up @@ -53,12 +53,34 @@ public int getDrawLimitMoveBackwardOverDrawAdjust(LayoutManager<Cell> layoutMana
}

@Override
public int getCellDisplacementFromSnapPosition(LayoutManager<Cell> layoutManager, int size, Cell cell) {
public int getDisplacementFromSnapPosition(LayoutManager<Cell> layoutManager, int size, Cell firstPosition, Cell lastPosition) {
final Integer firstDisplacement = getCellDisplacementFromSnapPosition(layoutManager, size, firstPosition);
final Integer lastDisplacement = getCellDisplacementFromSnapPosition(layoutManager, size, lastPosition);

if (firstDisplacement != null && lastDisplacement != null) {
if (Math.abs(firstDisplacement) < Math.abs(lastDisplacement)) {
return firstDisplacement;
} else {
return lastDisplacement;
}
} else if (firstDisplacement != null) {
return firstDisplacement;
} else if (lastDisplacement != null) {
return lastDisplacement;
}

return 0;
}

private Integer getCellDisplacementFromSnapPosition(LayoutManager<Cell> layoutManager, int size, Cell cell) {
if (cell == null) {
return null;
}
final int startSizePadding = layoutManager.getStartSizePadding();
final int currentCellStart = layoutManager.getCellStart(cell);
final int currentCellEnd = layoutManager.getCellEnd(cell);
if (currentCellStart < startSizePadding && currentCellEnd < startSizePadding + size) {
final int displacement = - currentCellStart;
final int displacement = startSizePadding - currentCellStart;
return displacement;
} else if (currentCellEnd > startSizePadding + size && currentCellStart > startSizePadding) {
final int displacement = startSizePadding + size - currentCellEnd;
Expand Down
Expand Up @@ -17,7 +17,7 @@ public interface SnapPositionInterface<Cell> {

public int getDrawLimitMoveBackwardOverDrawAdjust(final LayoutManager<Cell> layoutManager, final List<Cell> cells, final int size, final Cell cell);

public int getCellDisplacementFromSnapPosition(final LayoutManager<Cell> layoutManager, final int size, final Cell cell);
public int getDisplacementFromSnapPosition(final LayoutManager<Cell> layoutManager, final int size, final Cell firstPosition, Cell lastPosition);

public int getCellDistanceFromSnapPosition(final LayoutManager<Cell> layoutManager, final int size, final Cell cell);

Expand Down
Expand Up @@ -2,12 +2,12 @@

import android.view.View;

import java.util.List;

import mobi.parchment.widget.adapterview.LayoutManager;
import mobi.parchment.widget.adapterview.Move;
import mobi.parchment.widget.adapterview.ScrollDirectionManager;

import java.util.List;

/**
* Created by Emir Hasanbegovic on 2014-03-11.
*/
Expand All @@ -27,17 +27,29 @@ public int getDrawLimitMoveBackwardOverDrawAdjust(LayoutManager<Cell> layoutMana
}

@Override
public int getCellDisplacementFromSnapPosition(LayoutManager<Cell> layoutManager, int size, Cell cell) {
public int getDisplacementFromSnapPosition(LayoutManager<Cell> layoutManager, int size, Cell firstPosition, Cell lastPosition) {
final Integer firstDisplacement = getCellDisplacementFromSnapPosition(layoutManager, firstPosition);

if (firstDisplacement != null) {
return firstDisplacement;
}

return 0;
}

private Integer getCellDisplacementFromSnapPosition(final LayoutManager<Cell> layoutManager, final Cell cell) {
if (cell == null) {
return null;
}
final int startSizePadding = layoutManager.getStartSizePadding();
final int currentCellStart = layoutManager.getCellStart(cell);
final int displacement = startSizePadding - currentCellStart;
return displacement;
return startSizePadding - currentCellStart;
}

@Override
public int getCellDistanceFromSnapPosition(LayoutManager<Cell> layoutManager, int size, Cell cell) {
final int displacement = getCellDisplacementFromSnapPosition(layoutManager, size, cell);
return Math.abs(displacement);
return Math.abs(getCellDisplacementFromSnapPosition(layoutManager,cell));

}

@Override
Expand All @@ -55,7 +67,7 @@ public int getRedrawOffset(final ScrollDirectionManager scrollDirectionManager,
}

@Override
public int getAbsoluteSnapPosition(final LayoutManager<Cell> layoutManager, final int size, final int cellSize, final Move move) {
public int getAbsoluteSnapPosition(final LayoutManager<Cell> layoutManager, final int size, final int cellSize, final Move move) {
final int startSizePadding = layoutManager.getStartSizePadding();
return startSizePadding;
}
Expand Down
Expand Up @@ -44,23 +44,23 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
public void testCenterDisplacementNegative() {
final CenterSnapPosition<Cell> snapPosition = new CenterSnapPosition();
final Cell cell = new Cell(50, 100);
final int displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
final int displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(-25);
}

@Test
public void testCenterDisplacementIs0() {
final CenterSnapPosition<Cell> snapPosition = new CenterSnapPosition();
final Cell cell = new Cell(25, 75);
final int displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
final int displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(0);
}

@Test
public void testCenterDisplacementIsPositive() {
final CenterSnapPosition<Cell> snapPosition = new CenterSnapPosition();
final Cell cell = new Cell(0, 50);
final int displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
final int displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(25);
}

Expand All @@ -69,75 +69,75 @@ public void testCenterDisplacementIsPositive() {
public void testStartDisplacementIsNegative() {
final StartSnapPosition<Cell> snapPosition = new StartSnapPosition();
final Cell cell = new Cell(75, 100);
final int displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
final int displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(-75);
}

@Test
public void testStartDisplacementIs0() {
final StartSnapPosition<Cell> snapPosition = new StartSnapPosition();
final Cell cell = new Cell(0, 25);
final int displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
final int displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(0);
}

@Test
public void testStartDisplacementIsPositive() {
final StartSnapPosition<Cell> snapPosition = new StartSnapPosition();
final Cell cell = new Cell(-10, 15);
final int displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
final int displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(10);
}

@Test
public void testEndDisplacementIsNegative() {
final EndSnapPosition<Cell> snapPosition = new EndSnapPosition();
final Cell cell = new Cell(85,110);
final int displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
final int displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(-10);
}

@Test
public void testEndDisplacementIs0() {
final EndSnapPosition<Cell> snapPosition = new EndSnapPosition();
final Cell cell = new Cell(75,100);
final int displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
final int displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(0);
}

@Test
public void testEndDisplacementIsPositive() {
final EndSnapPosition<Cell> snapPosition = new EndSnapPosition();
final Cell cell = new Cell(65,90);
final int displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
final int displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(10);
}

@Test
public void testOnScreenDisplacementIsNegative() {
final OnScreenSnapPosition<Cell> snapPosition = new OnScreenSnapPosition();
final Cell cell = new Cell(90, 110);
final int displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
final int displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(-10);
}

@Test
public void testOnScreenDisplacementIs0() {
final OnScreenSnapPosition<Cell> snapPosition = new OnScreenSnapPosition();
Cell cell = new Cell(65, 100);
int displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
int displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(0);

cell = new Cell(0, 10);
displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(0);
}

@Test
public void testOnScreenDisplacementIsPositive() {
final OnScreenSnapPosition<Cell> snapPosition = new OnScreenSnapPosition();
final Cell cell = new Cell(-10,10);
final int displacement = snapPosition.getCellDisplacementFromSnapPosition(mLayoutManager, 100, cell);
final int displacement = snapPosition.getDisplacementFromSnapPosition(mLayoutManager, 100, cell, lastPosition);
assertThat(displacement).isEqualTo(10);
}

Expand Down

0 comments on commit 8f363e8

Please sign in to comment.