Search code examples
androidgridviewadapterbaseadapter

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!


Solution

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