listView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight,
int oldBottom) {
System.out.println("layout changed");
if (bottom > 0 && oldBottom > 0 && bottom != oldBottom) {
listView.removeOnLayoutChangeListener(this);
scrollRunnable.setBottomValue(bottom, oldBottom);
scrollRunnable.run();
listView.addOnLayoutChangeListener(this);
}
}
});
ScrollRunnable scrollRunnable = new ScrollRunnable();
class ScrollRunnable implements Runnable {
private int bottomValue;
private int oldBottomValue;
public void setBottomValue(int bottom, int oldBottom) {
this.bottomValue = bottom;
this.oldBottomValue = oldBottom;
}
@Override
public void run() {
final ViewGroup parent = (ViewGroup) listView.getParent();
parent.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top,
int right, int bottom, int oldLeft, int oldTop,
int oldRight, int oldBottom) {
parent.removeOnLayoutChangeListener(this);
// scrool2Bottom();
int result = bottomValue - oldBottomValue;
int scrollValue = result;
// listView.scrollBy(0, -result);
int firstVisiblePosition = listView
.getFirstVisiblePosition();
int firstVisibleTop = listView.getChildAt(0).getTop();
listView.setSelectionFromTop(firstVisiblePosition,
scrollValue + firstVisibleTop);
}
});
}
}