herbertD
herbertD

Reputation: 10965

Android, How to disable GridView's onItemSelected on some items?

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!

Upvotes: 0

Views: 1312

Answers (3)

anhtuannd
anhtuannd

Reputation: 962

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;
}

Upvotes: 1

herbertD
herbertD

Reputation: 10965

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.

Upvotes: 0

Shark
Shark

Reputation: 6416

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...

Upvotes: 0

Related Questions