0

It seems that the behavior of gridview's onitemselected is controlled by Android, how can I disable some items to callback the onItemSelected()?

my code:

@Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {

        if (view == null) return;

        Utils.log(TAG, "view = " + view.toString() + ",pos = " + position + " , id =" + id);

        //I want to disable onItemSelected after positon 3: (But I failed.)
        if (position > 3) {
            if (mLSV != null) {
                onItemSelected(mGridView,mLSV, mLastPosition, mLastSelectedId);
                return;
            }
        }

        if (!mGridView.isFocused()) return;

        if (mLSV != null) {
            mLSV.setBackgroundColor(CMainUI_Model.BG_COLOR); 
        }
        Utils.log(TAG, "onItemSelected, pos = " + position);

        mLSV = view;
        mLastPosition = position;
        mLastSelectedId = mGridView.getSelectedItemId();
    }

I use the onItemSelected() to changed the item's background like a focus as I navigate by D-pad. And I want not to call onItemSelected() after position 3 and the 'focus' stoped at position 3. Thanks!

herbertD
  • 10,657
  • 13
  • 50
  • 77
  • Can't you just ignore callback from some Views (add some kind of condition in the begining)? – olshevski Oct 15 '12 at 15:51
  • Thanks, I can ignore 'cause I use D-pad to navigate and I use onItemSelected to change the item's background before position 3. – herbertD Oct 15 '12 at 15:59

3 Answers3

1

It was quite long time ago, but overrides these methods in your Adapter may help:

@Override
public boolean isEnabled(int position) {
    // Check if position is enabled or not
    return true;
}

@Override
public boolean areAllItemsEnabled() {
    return false;
}
anhtuannd
  • 918
  • 9
  • 16
0

What about this?

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {

    if (view == null) return;

    Utils.log(TAG, "view = " + view.toString() + ",pos = " + position + " , id =" + id);

    //I want to disable onItemSelected after positon 3: 
    if (position > 3) return;

    if (!mGridView.isFocused()) return;

    if (mLSV != null) {
        mLSV.setBackgroundColor(CMainUI_Model.BG_COLOR); 
    }
    Utils.log(TAG, "onItemSelected, pos = " + position);

    mLSV = view;
    mLastPosition = position;
    mLastSelectedId = mGridView.getSelectedItemId();
}

Try this onItemSelected method...

Shark
  • 6,513
  • 3
  • 28
  • 50
  • I want to skip the action after position 3, such as set the item's background. I'm doing a app used by D-pad, not mouse. – herbertD Oct 15 '12 at 15:57
  • I've tried this and There are some problems: If you go to the right of position 3 by click D-pad RIGHT two times, You have to go back by pressing D-pad LEFT two times, That is boring. Thank you any way. – herbertD Oct 26 '12 at 03:44
0

I override the onKey() callback to disable some direction key events if the focus reach the limit. I think this is the best solution. Don't set onFocusChangedListener on the items of adapter 'because they will get lost or malformed by Android caching system: convertView.

herbertD
  • 10,657
  • 13
  • 50
  • 77