0

看来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。谢谢!

4

3 回答 3

1

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

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

@Override
public boolean areAllItemsEnabled() {
    return false;
}
于 2016-07-22T11:37:44.077 回答
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 方法...

于 2012-10-15T15:51:10.503 回答
0

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

于 2012-10-26T03:48:14.593 回答