Skip to content

Instantly share code, notes, and snippets.

@chrisbanes
Last active January 24, 2021 01:13
Show Gist options
  • Save chrisbanes/5004907 to your computer and use it in GitHub Desktop.
Save chrisbanes/5004907 to your computer and use it in GitHub Desktop.
Blocking ListView
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class BlockingListView extends ListView {
private boolean mBlockLayoutChildren;
public BlockingListView(Context context) {
super(context);
}
public BlockingListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setBlockLayoutChildren(boolean block) {
mBlockLayoutChildren = block;
}
@Override
protected void layoutChildren() {
if (!mBlockLayoutChildren) {
super.layoutChildren();
}
}
}
===========================
| USAGE
===========================
int firstVisPos = mListView.getFirstVisiblePosition();
View firstVisView = mListView.getChildAt(0);
int top = firstVisView != null ? firstVisView.getTop() : 0;
mListView.setBlockLayoutChildren(true);
int numberOfItemsAddedBeforeFirstVisible = ...;
mAdapter.swapCursor(...);
mListView.setBlockLayoutChildren(false);
mListView.setSelectionFromTop(firstVisPos + numberOfItemsAddedBeforeFirstVisible, top)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment