下面列出了android.widget.ListAdapter#isEnabled ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* {@inheritDoc}
*/
@Override
public void addTouchables( ArrayList<View> views ) {
final int count = getChildCount();
final int firstPosition = mFirstPosition;
final ListAdapter adapter = mAdapter;
if ( adapter == null ) {
return;
}
for ( int i = 0; i < count; i++ ) {
final View child = getChildAt( i );
if ( adapter.isEnabled( firstPosition + i ) ) {
views.add( child );
}
child.addTouchables( views );
}
}
@Override
public void addTouchables(ArrayList<View> views) {
final int count = getChildCount();
final int firstPosition = mFirstPosition;
final ListAdapter adapter = mAdapter;
if (adapter == null) {
return;
}
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (adapter.isEnabled(firstPosition + i)) {
views.add(child);
}
child.addTouchables(views);
}
}
/**
* {@inheritDoc}
*/
@Override
public void addTouchables(ArrayList<View> views) {
final int count = getChildCount();
final int firstPosition = mFirstPosition;
final ListAdapter adapter = mAdapter;
if (adapter == null) {
return;
}
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (adapter.isEnabled(firstPosition + i)) {
views.add(child);
}
child.addTouchables(views);
}
}
@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
boolean populated = super.dispatchPopulateAccessibilityEvent(event);
// If the item count is less than 15 then subtract disabled items from the count and
// position. Otherwise ignore disabled items.
if (!populated) {
int itemCount = 0;
int currentItemIndex = getSelectedItemPosition();
ListAdapter adapter = getAdapter();
if (adapter != null) {
final int count = adapter.getCount();
if (count < 15) {
for (int i = 0; i < count; i++) {
if (adapter.isEnabled(i)) {
itemCount++;
} else if (i <= currentItemIndex) {
currentItemIndex--;
}
}
} else {
itemCount = count;
}
}
event.setItemCount(itemCount);
event.setCurrentItemIndex(currentItemIndex);
}
return populated;
}
protected int lookForSelectablePosition(int position, boolean lookDown) {
ListAdapter adapter = this.mAdapter;
if (adapter == null || isInTouchMode()) {
return -1;
}
int count = adapter.getCount();
if (!this.mAreAllItemsSelectable) {
if (lookDown) {
position = Math.max(0, position);
while (position < count && !adapter.isEnabled(position)) {
position++;
}
} else {
position = Math.min(position, count - 1);
while (position >= 0 && !adapter.isEnabled(position)) {
position--;
}
}
if (position < 0 || position >= count) {
return -1;
}
return position;
} else if (position < 0 || position >= count) {
return -1;
} else {
return position;
}
}
/**
* Find a position that can be selected (i.e., is not a separator).
*
* @param position The starting position to look at.
* @param lookDown Whether to look down for other positions.
* @return The next selectable position starting at position and then searching either up or
* down. Returns {@link #INVALID_POSITION} if nothing can be found.
*/
@Override
int lookForSelectablePosition(int position, boolean lookDown) {
final ListAdapter adapter = mAdapter;
if (adapter == null || isInTouchMode()) {
return INVALID_POSITION;
}
final int count = adapter.getCount();
if (!mAreAllItemsSelectable) {
if (lookDown) {
position = Math.max(0, position);
while (position < count && !adapter.isEnabled(position)) {
position++;
}
} else {
position = Math.min(position, count - 1);
while (position >= 0 && !adapter.isEnabled(position)) {
position--;
}
}
if (position < 0 || position >= count) {
return INVALID_POSITION;
}
return position;
} else {
if (position < 0 || position >= count) {
return INVALID_POSITION;
}
return position;
}
}
/**
* Returns true if the item at the specified position is not a separator.
*
* @param position Position of the item whose data we want
*/
@Override
public boolean isEnabled(int position) {
for (ListAdapter piece : pieces) {
int size = piece.getCount();
if (position < size) {
return (piece.isEnabled(position));
}
position -= size;
}
return (false);
}
/**
* If the wrapped SpinnerAdapter is also a ListAdapter, delegate this call.
* Otherwise, return true.
*/
public boolean isEnabled(int position) {
final ListAdapter adapter = mListAdapter;
if (adapter != null) {
return adapter.isEnabled(position);
} else {
return true;
}
}
/**
* If the wrapped SpinnerAdapter is also a ListAdapter, delegate this call.
* Otherwise, return true.
*/
public boolean isEnabled(int position) {
final ListAdapter adapter = mListAdapter;
if (adapter != null) {
return adapter.isEnabled(position);
} else {
return true;
}
}
/**
* Find a position that can be selected (i.e., is not a separator).
*
* @param position
* The starting position to look at.
* @param lookDown
* Whether to look down for other positions.
* @return The next selectable position starting at position and then searching either up or down. Returns
* {@link #INVALID_POSITION} if nothing can be found.
*/
@Override
protected int lookForSelectablePosition( int position, boolean lookDown ) {
final ListAdapter adapter = mAdapter;
if ( adapter == null || isInTouchMode() ) {
return INVALID_POSITION;
}
final int count = adapter.getCount();
if ( !mAreAllItemsSelectable ) {
if ( lookDown ) {
position = Math.max( 0, position );
while ( position < count && !adapter.isEnabled( position ) ) {
position++;
}
} else {
position = Math.min( position, count - 1 );
while ( position >= 0 && !adapter.isEnabled( position ) ) {
position--;
}
}
if ( position < 0 || position >= count ) {
return INVALID_POSITION;
}
return position;
} else {
if ( position < 0 || position >= count ) {
return INVALID_POSITION;
}
return position;
}
}
@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
boolean populated = super.dispatchPopulateAccessibilityEvent(event);
// If the item count is less than 15 then subtract disabled items from the count and
// position. Otherwise ignore disabled items.
if (!populated) {
int itemCount = 0;
int currentItemIndex = getSelectedItemPosition();
ListAdapter adapter = getAdapter();
if (adapter != null) {
final int count = adapter.getCount();
if (count < 15) {
for (int i = 0; i < count; i++) {
if (adapter.isEnabled(i)) {
itemCount++;
} else if (i <= currentItemIndex) {
currentItemIndex--;
}
}
} else {
itemCount = count;
}
}
event.setItemCount(itemCount);
event.setCurrentItemIndex(currentItemIndex);
}
return populated;
}
/**
* If the wrapped SpinnerAdapter is also a ListAdapter, delegate this call.
* Otherwise, return true.
*/
public boolean isEnabled(int position) {
final ListAdapter adapter = mListAdapter;
if (adapter != null) {
return adapter.isEnabled(position);
} else {
return true;
}
}
@Override
public void onInitializeAccessibilityNodeInfo( View host, AccessibilityNodeInfoCompat info ) {
super.onInitializeAccessibilityNodeInfo( host, info );
final int position = getPositionForView( host );
final ListAdapter adapter = getAdapter();
if ( ( position == INVALID_POSITION ) || ( adapter == null ) ) {
return;
}
if ( !isEnabled() || !adapter.isEnabled( position ) ) {
return;
}
if ( position == getSelectedItemPosition() ) {
info.setSelected( true );
info.addAction( AccessibilityNodeInfoCompat.ACTION_CLEAR_SELECTION );
} else {
info.addAction( AccessibilityNodeInfoCompat.ACTION_SELECT );
}
if ( isClickable() ) {
info.addAction( AccessibilityNodeInfoCompat.ACTION_CLICK );
info.setClickable( true );
}
if ( isLongClickable() ) {
info.addAction( AccessibilityNodeInfoCompat.ACTION_LONG_CLICK );
info.setLongClickable( true );
}
}
/**
* Find a position that can be selected (i.e., is not a separator).
*
* @param position The starting position to look at.
* @param lookDown Whether to look down for other positions.
* @return The next selectable position starting at position and then searching either up or
* down. Returns {@link #INVALID_POSITION} if nothing can be found.
*/
@Override
int lookForSelectablePosition(int position, boolean lookDown) {
final ListAdapter adapter = mAdapter;
if (adapter == null || isInTouchMode()) {
return INVALID_POSITION;
}
final int count = adapter.getCount();
if (!mAreAllItemsSelectable) {
if (lookDown) {
position = Math.max(0, position);
while (position < count && !adapter.isEnabled(position)) {
position++;
}
} else {
position = Math.min(position, count - 1);
while (position >= 0 && !adapter.isEnabled(position)) {
position--;
}
}
if (position < 0 || position >= count) {
return INVALID_POSITION;
}
return position;
} else {
if (position < 0 || position >= count) {
return INVALID_POSITION;
}
return position;
}
}
/**
* If the wrapped SpinnerAdapter is also a ListAdapter, delegate this call.
* Otherwise, return true.
*/
public boolean isEnabled(int position) {
final ListAdapter adapter = mListAdapter;
if (adapter != null) {
return adapter.isEnabled(position);
} else {
return true;
}
}
private int lookForSelectablePositionOnScreen(int direction) {
int firstPosition = this.mFirstPosition;
int startPos;
ListAdapter adapter;
int pos;
if (direction == 130) {
if (this.mSelectedPosition != -1) {
startPos = this.mSelectedPosition + 1;
} else {
startPos = firstPosition;
}
if (startPos >= this.mAdapter.getCount()) {
return -1;
}
if (startPos < firstPosition) {
startPos = firstPosition;
}
int lastVisiblePos = getLastVisiblePosition();
adapter = getAdapter();
pos = startPos;
while (pos <= lastVisiblePos) {
if (adapter.isEnabled(pos) && getChildAt(pos - firstPosition).getVisibility() == 0) {
return pos;
}
pos++;
}
} else {
int last = (getChildCount() + firstPosition) - 1;
if (this.mSelectedPosition != -1) {
startPos = this.mSelectedPosition - 1;
} else {
startPos = (getChildCount() + firstPosition) - 1;
}
if (startPos < 0 || startPos >= this.mAdapter.getCount()) {
return -1;
}
if (startPos > last) {
startPos = last;
}
adapter = getAdapter();
pos = startPos;
while (pos >= firstPosition) {
if (adapter.isEnabled(pos) && getChildAt(pos - firstPosition).getVisibility() == 0) {
return pos;
}
pos--;
}
}
return -1;
}
/**
* If the wrapped SpinnerAdapter is also a ListAdapter, delegate this call. Otherwise,
* return true.
*/
public boolean isEnabled(int position) {
final ListAdapter adapter = mListAdapter;
return adapter == null || adapter.isEnabled(position);
}
@Override
public boolean performAccessibilityAction( View host, int action, Bundle arguments ) {
if ( super.performAccessibilityAction( host, action, arguments ) ) {
return true;
}
final int position = getPositionForView( host );
final ListAdapter adapter = getAdapter();
if ( ( position == INVALID_POSITION ) || ( adapter == null ) ) {
// Cannot perform actions on invalid items.
return false;
}
if ( !isEnabled() || !adapter.isEnabled( position ) ) {
// Cannot perform actions on disabled items.
return false;
}
final long id = getItemIdAtPosition( position );
switch ( action ) {
case AccessibilityNodeInfoCompat.ACTION_CLEAR_SELECTION: {
if ( getSelectedItemPosition() == position ) {
setSelection( INVALID_POSITION );
return true;
}
}
return false;
case AccessibilityNodeInfoCompat.ACTION_SELECT: {
if ( getSelectedItemPosition() != position ) {
setSelection( position );
return true;
}
}
return false;
case AccessibilityNodeInfoCompat.ACTION_CLICK: {
if ( isClickable() ) {
return performItemClick( host, position, id );
}
}
return false;
case AccessibilityNodeInfoCompat.ACTION_LONG_CLICK: {
if ( isLongClickable() ) {
return performLongPress( host, position, id );
}
}
return false;
}
return false;
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
if(DEBUG)
Log.v("PLA_ListView", "onFocusChanged");
int closetChildIndex = -1;
if (gainFocus && previouslyFocusedRect != null) {
//previouslyFocusedRect.offset(mScrollX, mScrollY);
previouslyFocusedRect.offset(getScrollX(), getScrollY());
final ListAdapter adapter = mAdapter;
// Don't cache the result of getChildCount or mFirstPosition here,
// it could change in layoutChildren.
if (adapter.getCount() < getChildCount() + mFirstPosition) {
mLayoutMode = LAYOUT_NORMAL;
layoutChildren();
}
// figure out which item should be selected based on previously
// focused rect
Rect otherRect = mTempRect;
int minDistance = Integer.MAX_VALUE;
final int childCount = getChildCount();
final int firstPosition = mFirstPosition;
for (int i = 0; i < childCount; i++) {
// only consider selectable views
if (!adapter.isEnabled(firstPosition + i)) {
continue;
}
View other = getChildAt(i);
other.getDrawingRect(otherRect);
offsetDescendantRectToMyCoords(other, otherRect);
int distance = getDistance(previouslyFocusedRect, otherRect, direction);
if (distance < minDistance) {
minDistance = distance;
closetChildIndex = i;
}
}
}
if (closetChildIndex >= 0) {
setSelection(closetChildIndex + mFirstPosition);
} else {
requestLayout();
}
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
int closetChildIndex = -1;
if (gainFocus && previouslyFocusedRect != null) {
//previouslyFocusedRect.offset(mScrollX, mScrollY);
previouslyFocusedRect.offset(getScrollX(), getScrollY());
final ListAdapter adapter = mAdapter;
// Don't cache the result of getChildCount or mFirstPosition here,
// it could change in layoutChildren.
if (adapter.getCount() < getChildCount() + mFirstPosition) {
mLayoutMode = LAYOUT_NORMAL;
layoutChildren();
}
// figure out which item should be selected based on previously
// focused rect
Rect otherRect = mTempRect;
int minDistance = Integer.MAX_VALUE;
final int childCount = getChildCount();
final int firstPosition = mFirstPosition;
for (int i = 0; i < childCount; i++) {
// only consider selectable views
if (!adapter.isEnabled(firstPosition + i)) {
continue;
}
View other = getChildAt(i);
other.getDrawingRect(otherRect);
offsetDescendantRectToMyCoords(other, otherRect);
int distance = getDistance(previouslyFocusedRect, otherRect, direction);
if (distance < minDistance) {
minDistance = distance;
closetChildIndex = i;
}
}
}
if (closetChildIndex >= 0) {
setSelection(closetChildIndex + mFirstPosition);
} else {
requestLayout();
}
}