下面列出了android.widget.ListView#CHOICE_MODE_NONE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* {@inheritDoc}
*/
@Override
public void setAdapter( ListAdapter adapter ) {
if ( adapter != null ) {
mAdapterHasStableIds = mAdapter.hasStableIds();
if ( mChoiceMode != ListView.CHOICE_MODE_NONE && mAdapterHasStableIds &&
mCheckedIdStates == null ) {
mCheckedIdStates = new LongSparseArray<Integer>();
}
}
if ( mCheckStates != null ) {
mCheckStates.clear();
}
if ( mCheckedIdStates != null ) {
mCheckedIdStates.clear();
}
}
/**
* Returns the set of checked items ids. The result is only valid if the choice mode has not been set to
* {@link #CHOICE_MODE_NONE} and the adapter has stable IDs. ({@link ListAdapter#hasStableIds()} == {@code true})
*
* @return A new array which contains the id of each checked item in the list.
*/
public long[] getCheckedItemIds() {
if ( mChoiceMode == ListView.CHOICE_MODE_NONE || mCheckedIdStates == null || mAdapter == null ) {
return new long[0];
}
final LongSparseArray<Integer> idStates = mCheckedIdStates;
final int count = idStates.size();
final long[] ids = new long[count];
for ( int i = 0; i < count; i++ ) {
ids[i] = idStates.keyAt( i );
}
return ids;
}
/**
* Change the list selection mode
*/
private void toggleChoiceMode() {
clearSelection();
final int currentMode = listView.getChoiceMode();
switch (currentMode) {
case ListView.CHOICE_MODE_NONE:
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Toast.makeText(this, "List choice mode: SINGLE", Toast.LENGTH_SHORT).show();
break;
case ListView.CHOICE_MODE_SINGLE:
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Toast.makeText(this, "List choice mode: MULTIPLE", Toast.LENGTH_SHORT).show();
break;
case ListView.CHOICE_MODE_MULTIPLE:
listView.setChoiceMode(ListView.CHOICE_MODE_NONE);
Toast.makeText(this, "List choice mode: NONE", Toast.LENGTH_SHORT).show();
break;
}
}
/**
* Set the selected position of the list.
* Only valid when {@link #isShowing()} == {@code true}.
*
* @param position List position to set as selected.
*/
public void setSelection(int position) {
DropDownListView list = mDropDownList;
if (isShowing() && list != null) {
list.mListSelectionHidden = false;
list.setSelection(position);
if (list.getChoiceMode() != ListView.CHOICE_MODE_NONE) {
list.setItemChecked(position, true);
}
}
}
/**
* Returns the set of checked items ids. The result is only valid if the choice mode has not been set to
* {@link #CHOICE_MODE_NONE}.
*
* @return A new array which contains the id of each checked item in the list.
*
* @deprecated Use {@link #getCheckedItemIds()} instead.
*/
@Deprecated
public long[] getCheckItemIds() {
// Use new behavior that correctly handles stable ID mapping.
if ( mAdapter != null && mAdapter.hasStableIds() ) {
return getCheckedItemIds();
}
// Old behavior was buggy, but would sort of work for adapters without stable IDs.
// Fall back to it to support legacy apps.
if ( mChoiceMode != ListView.CHOICE_MODE_NONE && mCheckStates != null && mAdapter != null ) {
final SparseBooleanArray states = mCheckStates;
final int count = states.size();
final long[] ids = new long[count];
final ListAdapter adapter = mAdapter;
int checkedCount = 0;
for ( int i = 0; i < count; i++ ) {
if ( states.valueAt( i ) ) {
ids[checkedCount++] = adapter.getItemId( states.keyAt( i ) );
}
}
// Trim array if needed. mCheckStates may contain false values
// resulting in checkedCount being smaller than count.
if ( checkedCount == count ) {
return ids;
} else {
final long[] result = new long[checkedCount];
System.arraycopy( ids, 0, result, 0, checkedCount );
return result;
}
}
return new long[0];
}
/**
* Set the selected position of the list.
* Only valid when {@link #isShowing()} == {@code true}.
*
* @param position List position to set as selected.
*/
public void setSelection(int position) {
DropDownListView list = mDropDownList;
if (isShowing() && list != null) {
list.mListSelectionHidden = false;
list.setSelection(position);
if (list.getChoiceMode() != ListView.CHOICE_MODE_NONE) {
list.setItemChecked(position, true);
}
}
}
/**
* Set the selected position of the list.
* Only valid when {@link #isShowing()} == {@code true}.
*
* @param position List position to set as selected.
*/
public void setSelection(int position) {
DropDownListView list = mDropDownList;
if (isShowing() && list != null) {
list.mListSelectionHidden = false;
list.setSelection(position);
if (list.getChoiceMode() != ListView.CHOICE_MODE_NONE) {
list.setItemChecked(position, true);
}
}
}
/**
* Set the selected position of the list.
* Only valid when {@link #isShowing()} == {@code true}.
*
* @param position List position to set as selected.
*/
public void setSelection(int position) {
DropDownListView list = mDropDownList;
if (isShowing() && list != null) {
list.mListSelectionHidden = false;
list.setSelection(position);
if (list.getChoiceMode() != ListView.CHOICE_MODE_NONE) {
list.setItemChecked(position, true);
}
}
}
/**
* Detect SimpleType of a Widget
*
* @param v Widget
* @return
*/
public static String getSimpleType(View v, String type, boolean alreadyCalled)
{
if (type.endsWith("null")) return NULL;
if (type.endsWith("RadioButton")) return RADIO;
if (type.endsWith("RadioGroup")) return RADIO_GROUP;
if (type.endsWith("CheckBox") || type.endsWith("CheckedTextView")) return CHECKBOX;
if (type.endsWith("ToggleButton")) return TOGGLE_BUTTON;
if (type.endsWith("MenuDropDownListView") || type.endsWith("IconMenuView") || type.endsWith("ActionMenuView")) return MENU_VIEW;
if (type.endsWith("ListMenuItemView") || type.endsWith("IconMenuItemView") || type.endsWith("ActionMenuItemView")) return MENU_ITEM;
if (type.endsWith("DatePicker")) return DATE_PICKER;
if (type.endsWith("TimePicker")) return TIME_PICKER;
if (type.endsWith("DialogTitle")) return DIALOG_VIEW;
if (type.endsWith("Button")) return BUTTON;
if (type.endsWith("EditText")) return EDIT_TEXT;
if (type.endsWith("SearchAutoComplete")) return SEARCH_BAR;
if (type.endsWith("Spinner")) {
Spinner s = (Spinner)v;
if (s.getCount() == 0) return EMPTY_SPINNER;
return SPINNER;
}
if (type.endsWith("SeekBar")) return SEEK_BAR;
if (v instanceof RatingBar && (!((RatingBar)v).isIndicator())) return RATING_BAR;
if (type.endsWith("TabHost")) return TAB_HOST;
//if (type.endsWith("ExpandedMenuView") || type.endsWith("AlertController$RecycleListView")) { return EXPAND_MENU; }
if (type.endsWith("ListView") || type.endsWith("ExpandedMenuView")) {
ListView l = (ListView)v;
if (l.getCount() == 0) return EMPTY_LIST;
if (l.getAdapter().getClass().getName().endsWith("PreferenceGroupAdapter")) {
return PREFERENCE_LIST;
}
switch (l.getChoiceMode()) {
case ListView.CHOICE_MODE_NONE: return LIST_VIEW;
case ListView.CHOICE_MODE_SINGLE: return SINGLE_CHOICE_LIST;
case ListView.CHOICE_MODE_MULTIPLE: return MULTI_CHOICE_LIST;
}
}
if (type.endsWith("AutoCompleteTextView")) return AUTOCOMPLETE_TEXTVIEW;
if (type.endsWith("TextView")) return TEXT_VIEW;
if (type.endsWith("ImageView")) return IMAGE_VIEW;
if (type.endsWith("LinearLayout")) return LINEAR_LAYOUT;
if (type.endsWith("RelativeLayout")) return RELATIVE_LAYOUT;
if (type.endsWith("SlidingDrawer")) return SLIDING_DRAWER;
if (type.endsWith("DrawerLayout")) return DRAWER_LAYOUT;
if ((v instanceof WebView) || type.endsWith("WebView")) return WEB_VIEW;
if (type.endsWith("TwoLineListItem")) return LIST_ITEM;
if (type.endsWith("NumberPicker")) return NUMBER_PICKER;
if (type.endsWith("NumberPickerButton")) return NUMBER_PICKER_BUTTON;
String parentType = v.getClass().getSuperclass().getName();
if (alreadyCalled == false && parentType != null) {
System.out.print(">>>>>> " + parentType);
return getSimpleType(v, parentType, true);
}
return "";
}
private void updateDisplayState() {
final State state = getDisplayState(this);
mDefaultColor = SettingsActivity.getPrimaryColor(getActivity());
int accentColor = SettingsActivity.getAccentColor();
if (mLastMode == state.derivedMode && mLastSortOrder == state.derivedSortOrder
&& mLastShowSize == state.showSize
&& mLastShowFolderSize == state.showFolderSize
&& mLastShowThumbnail == state.showThumbnail
&& mLastShowHiddenFiles == state.showHiddenFiles
&& (mLastShowColor != 0 && mLastShowColor == mDefaultColor)
&& (mLastShowAccentColor != 0 && mLastShowAccentColor == accentColor))
return;
boolean refreshData = mLastShowHiddenFiles != state.showHiddenFiles;
mLastMode = state.derivedMode;
mLastSortOrder = state.derivedSortOrder;
mLastShowSize = state.showSize;
mLastShowFolderSize = state.showFolderSize;
mLastShowThumbnail = state.showThumbnail;
mLastShowHiddenFiles = state.showHiddenFiles;
mLastShowColor = mDefaultColor;
mProgressBar.setColor(mLastShowColor);
mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
final int choiceMode;
if (state.allowMultiple) {
choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
} else {
choiceMode = ListView.CHOICE_MODE_NONE;
}
final int thumbSize;
if (state.derivedMode == MODE_GRID) {
thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
mListView.setAdapter(null);
mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
mGridView.setAdapter(mAdapter);
mGridView.setColumnWidth(thumbSize);
mGridView.setNumColumns(GridView.AUTO_FIT);
mGridView.setChoiceMode(choiceMode);
mCurrentView = mGridView;
} else if (state.derivedMode == MODE_LIST) {
thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
mGridView.setAdapter(null);
mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
mListView.setAdapter(mAdapter);
mListView.setChoiceMode(choiceMode);
mCurrentView = mListView;
} else {
throw new IllegalStateException("Unknown state " + state.derivedMode);
}
((BaseActivity) getActivity()).upadateActionItems(mCurrentView);
mThumbSize = new Point(thumbSize, thumbSize);
if(refreshData) {
onUserSortOrderChanged();
}
}
private void updateDisplayState() {
final State state = getDisplayState(this);
mDefaultColor = SettingsActivity.getPrimaryColor(getActivity());
int accentColor = SettingsActivity.getAccentColor();
if (mLastMode == state.derivedMode && mLastSortOrder == state.derivedSortOrder
&& mLastShowSize == state.showSize
&& mLastShowFolderSize == state.showFolderSize
&& mLastShowThumbnail == state.showThumbnail
&& mLastShowHiddenFiles == state.showHiddenFiles
&& (mLastShowColor != 0 && mLastShowColor == mDefaultColor)
&& (mLastShowAccentColor != 0 && mLastShowAccentColor == accentColor))
return;
boolean refreshData = mLastShowHiddenFiles != state.showHiddenFiles;
mLastMode = state.derivedMode;
mLastSortOrder = state.derivedSortOrder;
mLastShowSize = state.showSize;
mLastShowFolderSize = state.showFolderSize;
mLastShowThumbnail = state.showThumbnail;
mLastShowHiddenFiles = state.showHiddenFiles;
mLastShowColor = mDefaultColor;
mProgressBar.setColor(mLastShowColor);
mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
final int choiceMode;
if (state.allowMultiple) {
choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
} else {
choiceMode = ListView.CHOICE_MODE_NONE;
}
final int thumbSize;
if (state.derivedMode == MODE_GRID) {
thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
mListView.setAdapter(null);
mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
mGridView.setAdapter(mAdapter);
mGridView.setColumnWidth(thumbSize);
mGridView.setNumColumns(GridView.AUTO_FIT);
mGridView.setChoiceMode(choiceMode);
mCurrentView = mGridView;
} else if (state.derivedMode == MODE_LIST) {
thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
mGridView.setAdapter(null);
mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
mListView.setAdapter(mAdapter);
mListView.setChoiceMode(choiceMode);
mCurrentView = mListView;
} else {
throw new IllegalStateException("Unknown state " + state.derivedMode);
}
((BaseActivity) getActivity()).upadateActionItems(mCurrentView);
mThumbSize = new Point(thumbSize, thumbSize);
if(refreshData) {
onUserSortOrderChanged();
}
}
private void updateDisplayState() {
final State state = getDisplayState(this);
mDefaultColor = SettingsActivity.getPrimaryColor(getActivity());
int accentColor = SettingsActivity.getAccentColor();
if (mLastMode == state.derivedMode && mLastSortOrder == state.derivedSortOrder
&& mLastShowSize == state.showSize
&& mLastShowFolderSize == state.showFolderSize
&& mLastShowThumbnail == state.showThumbnail
&& mLastShowHiddenFiles == state.showHiddenFiles
&& (mLastShowColor != 0 && mLastShowColor == mDefaultColor)
&& (mLastShowAccentColor != 0 && mLastShowAccentColor == accentColor))
return;
boolean refreshData = mLastShowHiddenFiles != state.showHiddenFiles;
mLastMode = state.derivedMode;
mLastSortOrder = state.derivedSortOrder;
mLastShowSize = state.showSize;
mLastShowFolderSize = state.showFolderSize;
mLastShowThumbnail = state.showThumbnail;
mLastShowHiddenFiles = state.showHiddenFiles;
mLastShowColor = mDefaultColor;
mProgressBar.setColor(mLastShowColor);
mListView.setVisibility(state.derivedMode == MODE_LIST ? View.VISIBLE : View.GONE);
mGridView.setVisibility(state.derivedMode == MODE_GRID ? View.VISIBLE : View.GONE);
final int choiceMode;
if (state.allowMultiple) {
choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL;
} else {
choiceMode = ListView.CHOICE_MODE_NONE;
}
final int thumbSize;
if (state.derivedMode == MODE_GRID) {
thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
mListView.setAdapter(null);
mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
mGridView.setAdapter(mAdapter);
mGridView.setColumnWidth(thumbSize);
mGridView.setNumColumns(GridView.AUTO_FIT);
mGridView.setChoiceMode(choiceMode);
mCurrentView = mGridView;
} else if (state.derivedMode == MODE_LIST) {
thumbSize = getResources().getDimensionPixelSize(R.dimen.icon_size);
mGridView.setAdapter(null);
mGridView.setChoiceMode(ListView.CHOICE_MODE_NONE);
mListView.setAdapter(mAdapter);
mListView.setChoiceMode(choiceMode);
mCurrentView = mListView;
} else {
throw new IllegalStateException("Unknown state " + state.derivedMode);
}
((BaseActivity) getActivity()).upadateActionItems(mCurrentView);
mThumbSize = new Point(thumbSize, thumbSize);
if(refreshData) {
onUserSortOrderChanged();
}
}
/**
* Add a view as a child and make sure it is measured (if necessary) and positioned properly.
*
* @param child
* The view to add
* @param position
* The position of this child
* @param x
* The x position relative to which this view will be positioned
* @param flowDown
* If true, align left edge to x. If false, align right edge to x.
* @param childrenTop
* Top edge where children should be positioned
* @param selected
* Is this position selected?
* @param recycled
* Has this view been pulled from the recycle bin? If so it does not need to be remeasured.
*/
private void setupChild( View child, int position, int x, boolean flowDown, int childrenTop, boolean selected, boolean recycled ) {
final boolean isSelected = selected && shouldShowSelector();
final boolean updateChildSelected = isSelected != child.isSelected();
final int mode = mTouchMode;
final boolean isPressed = mode > TOUCH_MODE_DOWN && mode < TOUCH_MODE_SCROLL && mMotionPosition == position;
final boolean updateChildPressed = isPressed != child.isPressed();
final boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();
// Respect layout params that are already in the view. Otherwise make some up...
// noinspection unchecked
AbsHListView.LayoutParams p = (AbsHListView.LayoutParams) child.getLayoutParams();
if ( p == null ) {
p = (AbsHListView.LayoutParams) generateDefaultLayoutParams();
}
p.viewType = mAdapter.getItemViewType( position );
if ( ( recycled && !p.forceAdd ) || ( p.recycledHeaderFooter && p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER ) ) {
attachViewToParent( child, flowDown ? -1 : 0, p );
} else {
p.forceAdd = false;
if ( p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER ) {
p.recycledHeaderFooter = true;
}
addViewInLayout( child, flowDown ? -1 : 0, p, true );
}
if ( updateChildSelected ) {
child.setSelected( isSelected );
}
if ( updateChildPressed ) {
child.setPressed( isPressed );
}
if ( mChoiceMode != ListView.CHOICE_MODE_NONE && mCheckStates != null ) {
if ( child instanceof Checkable ) {
( (Checkable) child ).setChecked( mCheckStates.get( position ) );
} else if ( android.os.Build.VERSION.SDK_INT >= 11 ) {
child.setActivated( mCheckStates.get( position ) );
}
}
if ( needToMeasure ) {
int childHeightSpec = ViewGroup.getChildMeasureSpec( mHeightMeasureSpec, mListPadding.top + mListPadding.bottom, p.height );
int lpWidth = p.width;
int childWidthSpec;
if ( lpWidth > 0 ) {
childWidthSpec = MeasureSpec.makeMeasureSpec( lpWidth, MeasureSpec.EXACTLY );
} else {
childWidthSpec = MeasureSpec.makeMeasureSpec( 0, MeasureSpec.UNSPECIFIED );
}
child.measure( childWidthSpec, childHeightSpec );
} else {
cleanupLayoutState( child );
}
final int w = child.getMeasuredWidth();
final int h = child.getMeasuredHeight();
final int childLeft = flowDown ? x : x - w;
if ( needToMeasure ) {
final int childBottom = childrenTop + h;
final int childRight = childLeft + w;
child.layout( childLeft, childrenTop, childRight, childBottom );
} else {
child.offsetLeftAndRight( childLeft - child.getLeft() );
child.offsetTopAndBottom( childrenTop - child.getTop() );
}
if ( mCachingStarted && !child.isDrawingCacheEnabled() ) {
child.setDrawingCacheEnabled( true );
}
if( android.os.Build.VERSION.SDK_INT >= 11 ) {
if ( recycled && ( ( (AbsHListView.LayoutParams) child.getLayoutParams() ).scrappedFromPosition ) != position ) {
child.jumpDrawablesToCurrentState();
}
}
}
/**
* Sets the checked state of the specified position. The is only valid if the choice mode has been set to
* {@link #CHOICE_MODE_SINGLE} or {@link #CHOICE_MODE_MULTIPLE}.
*
* @param position
* The item whose checked state is to be checked
* @param value
* The new checked state for the item
*/
public void setItemChecked( int position, boolean value ) {
if ( mChoiceMode == ListView.CHOICE_MODE_NONE ) {
return;
}
// Start selection mode if needed. We don't need to if we're unchecking something.
if ( android.os.Build.VERSION.SDK_INT >= 11 ) {
if ( value && mChoiceMode == ListView.CHOICE_MODE_MULTIPLE_MODAL && mChoiceActionMode == null ) {
if ( mMultiChoiceModeCallback == null ||
!((MultiChoiceModeWrapper)mMultiChoiceModeCallback).hasWrappedCallback() ) {
throw new IllegalStateException( "AbsListView: attempted to start selection mode " +
"for CHOICE_MODE_MULTIPLE_MODAL but no choice mode callback was " +
"supplied. Call setMultiChoiceModeListener to set a callback." );
}
mChoiceActionMode = startActionMode( (MultiChoiceModeWrapper)mMultiChoiceModeCallback );
}
}
if ( mChoiceMode == ListView.CHOICE_MODE_MULTIPLE
|| ( android.os.Build.VERSION.SDK_INT >= 11 && mChoiceMode == ListView.CHOICE_MODE_MULTIPLE_MODAL ) ) {
boolean oldValue = mCheckStates.get( position );
mCheckStates.put( position, value );
if ( mCheckedIdStates != null && mAdapter.hasStableIds() ) {
if ( value ) {
mCheckedIdStates.put( mAdapter.getItemId( position ), position );
} else {
mCheckedIdStates.delete( mAdapter.getItemId( position ) );
}
}
if ( oldValue != value ) {
if ( value ) {
mCheckedItemCount++;
} else {
mCheckedItemCount--;
}
}
if ( mChoiceActionMode != null ) {
final long id = mAdapter.getItemId( position );
((MultiChoiceModeWrapper)mMultiChoiceModeCallback).onItemCheckedStateChanged( (ActionMode) mChoiceActionMode, position, id, value );
}
} else {
boolean updateIds = mCheckedIdStates != null && mAdapter.hasStableIds();
// Clear all values if we're checking something, or unchecking the currently
// selected item
if ( value || isItemChecked( position ) ) {
mCheckStates.clear();
if ( updateIds ) {
mCheckedIdStates.clear();
}
}
// this may end up selecting the value we just cleared but this way
// we ensure length of mCheckStates is 1, a fact getCheckedItemPosition relies on
if ( value ) {
mCheckStates.put( position, true );
if ( updateIds ) {
mCheckedIdStates.put( mAdapter.getItemId( position ), position );
}
mCheckedItemCount = 1;
} else if ( mCheckStates.size() == 0 || !mCheckStates.valueAt( 0 ) ) {
mCheckedItemCount = 0;
}
}
// Do not generate a data change while we are in the layout phase
if ( !mInLayout && !mBlockLayoutRequests ) {
mDataChanged = true;
rememberSyncState();
requestLayout();
}
}
/**
* Returns the checked state of the specified position. The result is only valid if the choice mode has been set to
* {@link #CHOICE_MODE_SINGLE} or {@link #CHOICE_MODE_MULTIPLE}.
*
* @param position
* The item whose checked state to return
* @return The item's checked state or <code>false</code> if choice mode is invalid
*
* @see #setChoiceMode(int)
*/
public boolean isItemChecked( int position ) {
if ( mChoiceMode != ListView.CHOICE_MODE_NONE && mCheckStates != null ) {
return mCheckStates.get( position );
}
return false;
}
/**
* Returns the set of checked items in the list. The result is only valid if the choice mode has not been set to
* {@link #CHOICE_MODE_NONE}.
*
* @return A SparseBooleanArray which will return true for each call to get(int position) where position is a position in the
* list, or <code>null</code> if the choice mode is set to {@link #CHOICE_MODE_NONE}.
*/
public SparseBooleanArray getCheckedItemPositions() {
if ( mChoiceMode != ListView.CHOICE_MODE_NONE ) {
return mCheckStates;
}
return null;
}