Last active
January 24, 2021 01:13
-
-
Save chrisbanes/5004907 to your computer and use it in GitHub Desktop.
Blocking ListView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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