Android,如何在某些项目上禁用GridView onItemSelected?

时间:2012-10-15 15:47:52

标签: android gridview adapter baseadapter

似乎gridview的onitemselected的行为是由Android控制的,如何禁用某些项来回调onItemSelected()?

我的代码:

@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();
    }

当我通过D-pad导航时,我使用onItemSelected()来改变项目的背景,就像焦点一样。我想不要在位置3之后调用onItemSelected()并且将'焦点'停在位置3处。谢谢!

3 个答案:

答案 0 :(得分:1)

很久以前,但在适配器中覆盖这些方法可能有所帮助:

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

@Override
public boolean areAllItemsEnabled() {
    return false;
}

答案 1 :(得分:0)

这个怎么样?

@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();
}

试试这个onItemSelected方法...

答案 2 :(得分:0)

如果焦点达到限制,我会覆盖onKey()回调以禁用某些方向键事件。我认为这是最好的解决方案。不要在适配器的项目上设置onFocusChangedListener,因为Android缓存系统会丢失或格式错误:convertView。