解决listview底部变化遮盖

来源:互联网 发布:淘宝全球购直播申请 编辑:程序博客网 时间:2024/06/05 06:10


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);
}
});
}
}

0 0