Skip to content

Commit

Permalink
Fix layout issues (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
natario1 authored Oct 11, 2017
1 parent cf52992 commit 7e64c22
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,15 @@ public void onSpaceAvailable(DocumentColumn child, int space) {
public void onSpaceOver(DocumentColumn child) {
// This can be called by child TextViews or EditText that grown
// to be too small. See DocumentTextHelper.
mLog.w("notifyTooSmall:", "one of our children says is smaller than he would like to.");
getRoot().onSpaceOver(this);
// Go out of the layout pass... see onSpaceAvailable
mLog.w("onSpaceOver:", "one of our children says is smaller than he would like to.");
post(new Runnable() {
@Override
public void run() {
mLog.v("onSpaceOver:", "Performing.");
getRoot().onSpaceOver(DocumentColumn.this);
}
});
}

//endregion
Expand Down Expand Up @@ -203,14 +210,24 @@ public void onLayoutChange(View view, int left, int top, int right, int bottom,
int oldHeight = oldBottom - oldTop;
int newHeight = bottom - top;
mAvailableSpace = mHeightBound - newHeight;
mLog.v("onLayoutChange:", "oldHeight:", oldHeight, "newHeight:", newHeight, "avSpace:", mAvailableSpace);
final int pn = getRoot().getNumber();
mLog.v("onLayoutChange:", "page:", pn, "oldHeight:", oldHeight, "newHeight:", newHeight, "avSpace:", mAvailableSpace);

if (oldHeight == 0) return; // First pass.
if (newHeight == oldHeight) return; // Not really changed. This happens.
if (newHeight > oldHeight) return; // Nothing to do, hope we get calls to notifyTooSmall.
if (mAvailableSpace <= 0) return;
mLog.i("onLayoutChange:", "dispatching onSpaceAvailable.");
getRoot().onSpaceAvailable(this, mAvailableSpace);

// Go out of the layout pass, it's not safe to pass views around during layout,
// even if you use addViewInLayout or removeViewInLayout.
final int availableSpace = mAvailableSpace;
post(new Runnable() {
@Override
public void run() {
mLog.i("onLayoutChange:", "page:", pn, "dispatching onSpaceAvailable.");
getRoot().onSpaceAvailable(DocumentColumn.this, availableSpace);
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class DocumentPage extends LinearLayout implements Container<DocumentPager, Docu
// bound when measuring themselves, so it's not important.
// We also want width=0 and gravity=1.
DocumentColumn column = new DocumentColumn(context, i + 1, columnSize[0], columnSize[1]);
addView(column, new LayoutParams(0, columnSize[1], 1));
// addView(column, new LayoutParams(0, columnSize[1], 1));
addView(column, new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
mColumns.add(column);
}
}
Expand Down Expand Up @@ -274,17 +275,19 @@ public void onSpaceAvailable(DocumentColumn child, int space) {
if (!first) {
mLog.v("onSpaceAvailable:", "trying to pass to column", child.getNumber() - 1);
DocumentColumn previous = mColumns.get(which - 1);
if (tryPassFirstViewToPrevious(child, previous)) return;
while (tryPassFirstViewToPrevious(child, previous)) {} // Try until it stops
}

// Check if the next page wants to give this page its first child.
if (!last) {
mLog.v("onSpaceAvailable:", "trying to accept from column", child.getNumber() + 1);
DocumentColumn next = mColumns.get(which + 1);
if (tryPassFirstViewToPrevious(next, child)) return;
while (tryPassFirstViewToPrevious(next, child)) {} // try until it stops
} else {
// TODO: the 'space' value at this point might not be meaningful
// (might be bigger if we gave something to previous column). But it's unused
getRoot().onSpaceAvailable(this, space);
}

if (last) getRoot().onSpaceAvailable(this, space);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,23 +323,24 @@ public void onSpaceAvailable(DocumentPage child, int space) {
LOG.i("onSpaceAvailable:", "fromPage:", child.getNumber());

synchronized (mLock) {
int which = mPages.indexOf(child);
boolean first = which == 0;
boolean last = which == getPageCount() - 1;
int index = mPages.indexOf(child);
boolean first = index == 0;

// Check if the previous page wants our first child.
// TODO: this is a useless check if the first child was not the one collapsing.
if (!first) {
LOG.v("onSpaceAvailable:", "trying to pass to page", child.getNumber() - 1);
DocumentPage previous = mPages.get(which - 1);
if (tryPassFirstViewToPrevious(child, previous)) return;
DocumentPage previous = mPages.get(index - 1);
while (tryPassFirstViewToPrevious(child, previous)) {}
}

// Check if the next page wants to give this page its first child.
if (!last) {
index = mPages.indexOf(child);
boolean last = index == getPageCount() - 1;
if (index >= 0 && !last) {
LOG.v("onSpaceAvailable:", "trying to accept from page", child.getNumber() + 1);
DocumentPage next = mPages.get(which + 1);
if (tryPassFirstViewToPrevious(next, child)) return;
DocumentPage next = mPages.get(index + 1);
while (tryPassFirstViewToPrevious(next, child)) {}
}
}
}
Expand Down

0 comments on commit 7e64c22

Please sign in to comment.