下面列出了android.util.SparseBooleanArray#keyAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* <p>Applies the columns collapse status to a new row added to this
* table. This method is invoked by PassThroughHierarchyChangeListener
* upon child insertion.</p>
*
* <p>This method only applies to {@link android.widget.TableRow}
* instances.</p>
*
* @param child the newly added child
*/
private void trackCollapsedColumns(View child) {
if (child instanceof TableRow) {
final TableRow row = (TableRow) child;
final SparseBooleanArray collapsedColumns = mCollapsedColumns;
final int count = collapsedColumns.size();
for (int i = 0; i < count; i++) {
int columnIndex = collapsedColumns.keyAt(i);
boolean isCollapsed = collapsedColumns.valueAt(i);
// the collapse status is set only when the column should be
// collapsed; otherwise, this might affect the default
// visibility of the row's children
if (isCollapsed) {
row.setColumnCollapsed(columnIndex, isCollapsed);
}
}
}
}
/**
* Use this when an item has been deleted, to move the check state of all following items up one
* step. If you have a choiceMode which is not none, this method must be called when the order of
* items changes in an underlying adapter which does not have stable IDs (see {@link
* android.widget.ListAdapter#hasStableIds()}). This is because without IDs, the ListView has no
* way of knowing which items have moved where, and cannot update the check state accordingly.
* <p/>
* See also further comments on {@link #moveCheckState(int, int)}.
*
* @param position
*/
public void removeCheckState(int position) {
SparseBooleanArray cip = getCheckedItemPositions();
if (cip.size() == 0) {
return;
}
int[] runStart = new int[cip.size()];
int[] runEnd = new int[cip.size()];
int rangeStart = position;
int rangeEnd = cip.keyAt(cip.size() - 1) + 1;
int runCount = buildRunList(cip, rangeStart, rangeEnd, runStart, runEnd);
for (int i = 0; i != runCount; i++) {
if (! (runStart[i] == position || (runEnd[i] < runStart[i] && runEnd[i] > position))) {
// Only set a new check mark in front of this run if it does
// not contain the deleted position. If it does, we only need
// to make it one check mark shorter at the end.
setItemChecked(rotate(runStart[i], - 1, rangeStart, rangeEnd), true);
}
setItemChecked(rotate(runEnd[i], - 1, rangeStart, rangeEnd), false);
}
}
private int[] getSelectedIndices(ListView listView) {
SparseBooleanArray sparseArray = listView.getCheckedItemPositions();
int selectedCount = 0;
for (int i = 0; i < sparseArray.size(); ++i) {
if (sparseArray.valueAt(i)) {
selectedCount++;
}
}
int[] indices = new int[selectedCount];
for (int i = 0, j = 0; i < sparseArray.size(); ++i) {
if (sparseArray.valueAt(i)) {
indices[j++] = sparseArray.keyAt(i);
}
}
return indices;
}
/**
* Use this when an item has been deleted, to move the check state of all
* following items up one step. If you have a choiceMode which is not none,
* this method must be called when the order of items changes in an
* underlying adapter which does not have stable IDs (see
* {@link ListAdapter#hasStableIds()}). This is because without IDs, the
* ListView has no way of knowing which items have moved where, and cannot
* updateCache the check state accordingly.
*
* See also further comments on {@link #moveCheckState(int, int)}.
*
* @param position
*/
public void removeCheckState(int position) {
SparseBooleanArray cip = getCheckedItemPositions();
if (cip.size() == 0)
return;
int[] runStart = new int[cip.size()];
int[] runEnd = new int[cip.size()];
int rangeEnd = cip.keyAt(cip.size() - 1) + 1;
int runCount = buildRunList(cip, position, rangeEnd, runStart, runEnd);
for (int i = 0; i != runCount; i++) {
if (!(runStart[i] == position || (runEnd[i] < runStart[i] && runEnd[i] > position))) {
// Only set a new check mark in front of this run if it does
// not contain the deleted position. If it does, we only need
// to make it one check mark shorter at the end.
setItemChecked(rotate(runStart[i], -1, position, rangeEnd), true);
}
setItemChecked(rotate(runEnd[i], -1, position, rangeEnd), false);
}
}
private static int[] getSelectedIndices(ListView listView) {
SparseBooleanArray sparseArray = listView.getCheckedItemPositions();
int selectedCount = 0;
for (int i = 0; i < sparseArray.size(); ++i) {
if (sparseArray.valueAt(i)) {
selectedCount++;
}
}
int[] indices = new int[selectedCount];
for (int i = 0, j = 0; i < sparseArray.size(); ++i) {
if (sparseArray.valueAt(i)) {
indices[j++] = sparseArray.keyAt(i);
}
}
return indices;
}
private void cleanUpArrayForUser(SparseBooleanArray array, int removedUserId) {
for (int i = array.size() - 1; i >= 0; i--) {
final int uid = array.keyAt(i);
final int userId = UserHandle.getUserId(uid);
if (userId == removedUserId) {
array.removeAt(i);
}
}
}
protected void deleteSelectedItems() {
SparseBooleanArray checkedItems = getListView().getCheckedItemPositions();
for (int i = checkedItems.size() - 1; i > -1; --i) {
int position = checkedItems.keyAt(i);
boolean checked = checkedItems.get(position);
if (checked) {
getDescriptorList().remove(position);
}
}
}
private void uncheckAllItems() {
if( listView == null ) {
return;
}
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i)) {
listView.setItemChecked(position, false);
}
}
}
private static int insertionIndexForKey(@NonNull SparseBooleanArray sba, int key) {
int low = 0;
int high = sba.size();
while (high - low > 0) {
int middle = (low + high) >> 1;
if (sba.keyAt(middle) < key)
low = middle + 1;
else
high = middle;
}
return low;
}
private static int insertionIndexForKey(SparseBooleanArray sba, int key) {
int low = 0;
int high = sba.size();
while (high - low > 0) {
int middle = (low + high) >> 1;
if (sba.keyAt(middle) < key)
low = middle + 1;
else
high = middle;
}
return low;
}
/**
* Show a message giving the selected item captions
*/
private void showSelectedItems() {
final StringBuffer sb = new StringBuffer("Selection: ");
// Get an array that tells us for each position whether the item is
// checked or not
// --
final SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
if (checkedItems == null) {
Toast.makeText(this, "No selection info available", Toast.LENGTH_LONG).show();
return;
}
// For each element in the status array
// --
boolean isFirstSelected = true;
final int checkedItemsCount = checkedItems.size();
for (int i = 0; i < checkedItemsCount; ++i) {
// This tells us the item position we are looking at
// --
final int position = checkedItems.keyAt(i);
// This tells us the item status at the above position
// --
final boolean isChecked = checkedItems.valueAt(i);
if (isChecked) {
if (!isFirstSelected) {
sb.append(", ");
}
sb.append(data.get(position).getCaption());
isFirstSelected = false;
}
}
// Show a message with the countries that are selected
// --
Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
}
private static int findFirstSetIndex(@NonNull SparseBooleanArray sba, int rangeStart, int rangeEnd) {
int size = sba.size();
int i = insertionIndexForKey(sba, rangeStart);
while (i < size && sba.keyAt(i) < rangeEnd && !sba.valueAt(i))
i++;
if (i == size || sba.keyAt(i) >= rangeEnd)
return -1;
return i;
}
private void hidePopUpMenu(ViewHolder holder) {
SparseBooleanArray checkedItems = mListView.getCheckedItemPositions();
if (checkedItems.size() > 0) {
for (int i = 0; i < checkedItems.size(); i++) {
int key = checkedItems.keyAt(i);
if (checkedItems.get(key)) {
holder.popup.setVisibility(View.INVISIBLE);
}
}
} else {
holder.popup.setVisibility(View.VISIBLE);
}
}
private static int insertionIndexForKey(SparseBooleanArray sba, int key) {
int low = 0;
int high = sba.size();
while (high - low > 0) {
int middle = (low + high) >> 1;
if (sba.keyAt(middle) < key)
low = middle + 1;
else
high = middle;
}
return low;
}
private void hidePopUpMenu(ViewHolder holder) {
SparseBooleanArray checkedItems = mListView.getCheckedItemPositions();
if (checkedItems.size() > 0) {
for (int i = 0; i < checkedItems.size(); i++) {
int key = checkedItems.keyAt(i);
if (checkedItems.get(key)) {
holder.popup.setVisibility(View.INVISIBLE);
}
}
} else {
holder.popup.setVisibility(View.VISIBLE);
}
}
private void hidePopUpMenu(ViewHolder holder) {
SparseBooleanArray checkedItems = mListView.getCheckedItemPositions();
if (checkedItems.size() > 0) {
for (int i = 0; i < checkedItems.size(); i++) {
int key = checkedItems.keyAt(i);
if (checkedItems.get(key)) {
holder.popup.setVisibility(View.INVISIBLE);
}
}
} else {
holder.popup.setVisibility(View.VISIBLE);
}
}
private long[] getChecked(SparseBooleanArray array) {
ArrayList<Long> checked = new ArrayList<Long>();
for (int i = 0; i < array.size(); i++) {
int key = array.keyAt(i);
if (array.get(key)) {
checked.add(trackIds[key]);
}
}
long[] result = new long[checked.size()];
for (int i = 0; i < checked.size(); i++) {
result[i] = checked.get(i).longValue();
}
return result;
}
@Override
void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (isMultiChoiceList()) {
ListAdapter adapter = choicesList.getAdapter();
boolean[] checkedStates = new boolean[adapter.getCount()];
SparseBooleanArray checkedPositions = choicesList.getCheckedItemPositions();
for (int i = 0; i < checkedPositions.size(); i++) {
if (checkedPositions.valueAt(i)) {
checkedStates[checkedPositions.keyAt(i)] = true;
}
}
outState.putBooleanArray(KEY_ITEM_CHECKED_STATES, checkedStates);
}
}
private static int findFirstSetIndex(SparseBooleanArray sba, int rangeStart, int rangeEnd) {
int size = sba.size();
int i = insertionIndexForKey(sba, rangeStart);
while (i < size && sba.keyAt(i) < rangeEnd && !sba.valueAt(i))
i++;
if (i == size || sba.keyAt(i) >= rangeEnd)
return -1;
return i;
}
private static int findFirstSetIndex(SparseBooleanArray sba, int rangeStart, int rangeEnd) {
int size = sba.size();
int i = insertionIndexForKey(sba, rangeStart);
while (i < size && sba.keyAt(i) < rangeEnd && !sba.valueAt(i))
i++;
if (i == size || sba.keyAt(i) >= rangeEnd)
return -1;
return i;
}